Flutter > 提供商 > Firebase |无法流式传输一个对象,但另一个流式传输良好并且它们都非常相似
Flutter > Provider > Firebase | can't stream one object but another is streaming well and both of them really similar
我正在使用 flutter 和 provider 以及 firbase。
当流式传输产品列表时,它流式传输良好。
但是当想流订单时return 没什么事。
请帮助我
这是产品和订单class
class Order {
final String id;
final String uid;
final String email;
final String status;
final int total;
Order({
this.id,
this.uid,
this.email,
this.status,
this.total,
});
Order.fromFire(DocumentSnapshot doc)
: id = doc.documentID,
uid = doc['uid'],
email = doc['email'],
status = doc['status'],
total = doc['total'];
}
import 'package:cloud_firestore/cloud_firestore.dart';
class Product {
final String id;
final String name;
final int price;
Product({
this.id,
this.name,
this.price,
});
Product.fromFire(DocumentSnapshot doc)
: id = doc.documentID,
name = doc['name'],
price = doc['price'];
}
这里是溪流
Stream<List<Product>> getProductList() {
return products.snapshots().map((snapShot) => snapShot.documents
.map(
(document) => Product.fromFire(document),
)
.toList());
}
Stream<List<Order>> getOrderList() {
return orders.snapshots().map((snapShot) => snapShot.documents
.map(
(document) => Order.fromFire(document),
)
.toList());
}
这里是供应商
FirebaseService _db = FirebaseService();
StreamProvider(
create: (context) => _db.getProductList(),
catchError: (_, __) => null,
),
StreamProvider(
create: (context) => _db.getOrderList(),
catchError: (_, __) => null,
),
并使用
读取流
List<Order> orderList = Provider.of<List<Order>>(context) ?? [];
List<Product> productList = Provider.of<List<Product>>(context) ?? [];
但产品长度为 5,订单长度为 0,并且在 firestore 中有一些订单
I/flutter (15121): product length 5
I/flutter (15121): order length 0
我发现我的错误是在 Order 模型中我为订单总价定义了 int,当在服务中下订单时,总价是双倍!:)
class Order {
final String id;
final String uid;
final String email;
final String status;
final int total;
...
placeOrder(
FirebaseUser user,
Address address,
List<Product> products,
) async {
String orderid = user.email + ' ' + DateTime.now().toString();
double total = 0;
for (int i = 0; i < products.length; i++) {
total = products[i].price + total;
}
...
我正在使用 flutter 和 provider 以及 firbase。 当流式传输产品列表时,它流式传输良好。 但是当想流订单时return 没什么事。 请帮助我
这是产品和订单class
class Order {
final String id;
final String uid;
final String email;
final String status;
final int total;
Order({
this.id,
this.uid,
this.email,
this.status,
this.total,
});
Order.fromFire(DocumentSnapshot doc)
: id = doc.documentID,
uid = doc['uid'],
email = doc['email'],
status = doc['status'],
total = doc['total'];
}
import 'package:cloud_firestore/cloud_firestore.dart';
class Product {
final String id;
final String name;
final int price;
Product({
this.id,
this.name,
this.price,
});
Product.fromFire(DocumentSnapshot doc)
: id = doc.documentID,
name = doc['name'],
price = doc['price'];
}
这里是溪流
Stream<List<Product>> getProductList() {
return products.snapshots().map((snapShot) => snapShot.documents
.map(
(document) => Product.fromFire(document),
)
.toList());
}
Stream<List<Order>> getOrderList() {
return orders.snapshots().map((snapShot) => snapShot.documents
.map(
(document) => Order.fromFire(document),
)
.toList());
}
这里是供应商
FirebaseService _db = FirebaseService();
StreamProvider(
create: (context) => _db.getProductList(),
catchError: (_, __) => null,
),
StreamProvider(
create: (context) => _db.getOrderList(),
catchError: (_, __) => null,
),
并使用
读取流 List<Order> orderList = Provider.of<List<Order>>(context) ?? [];
List<Product> productList = Provider.of<List<Product>>(context) ?? [];
但产品长度为 5,订单长度为 0,并且在 firestore 中有一些订单
I/flutter (15121): product length 5
I/flutter (15121): order length 0
我发现我的错误是在 Order 模型中我为订单总价定义了 int,当在服务中下订单时,总价是双倍!:)
class Order {
final String id;
final String uid;
final String email;
final String status;
final int total;
...
placeOrder(
FirebaseUser user,
Address address,
List<Product> products,
) async {
String orderid = user.email + ' ' + DateTime.now().toString();
double total = 0;
for (int i = 0; i < products.length; i++) {
total = products[i].price + total;
}
...