使用firebase后端的流式购物车总价



我被卡住了,我正试图从购物车中流式传输产品价格并进行汇总以获得总额。。。这是我的密码。。。

Container(
alignment: Alignment.bottomCenter,
child: StreamBuilder(
stream: firebaseFirestoreOperations.usersRef
.doc(firebaseFirestoreOperations.getUserId())
.collection('Cart')
.doc('9oNFPyrUPN9wORFsyqvY')
.snapshots(),
builder: (context, snapshot) {
var price = snapshot.data['price'];
return CartBottomBar(
totalPrice: price,
);
}),
),
Container(
alignment: Alignment.bottomCenter,
child: StreamBuilder(
stream: firebaseFirestoreOperations.usersRef
.doc(firebaseFirestoreOperations.getUserId())
.collection('Cart')
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {

if (snapshot.connectionState == ConnectionState.done) {
let total = 0;
//               snapshot.docs.forEach((doc) => {
snapshot.forEach((doc) => {
total += doc.data()['price'];
});

return Text('$${total}  ');
}

return Text('Loading');



}),
),

检查此参考:https://firebase.flutter.dev/docs/firestore/usage#realtime-更改

最新更新