实现了StreamBuilder来显示购物车中的物品的更新价格。当一个或两个项目在购物车中时,一切都按预期工作,但当添加第三个项目时,我正确地获得第三个项目,但购物车中的第一个和第二个项目显示以下错误:“坏状态:DocumentSnapshotPlatform中不存在字段。导致错误的相关小部件是StreamBuilder<DocumentSnapshot<Map<String,dynamic>>>"。我相信这是一个定时错误(异步/等待),或者需要某种暂停。这是我的代码:
class PriceUpdaterWidget extends StatelessWidget {
const PriceUpdaterWidget({
Key? key,
required this.loginService,
required this.code,
}) : super(key: key);
final LoginService loginService;
final String? code;
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance
.collection('shoppers')
.doc(loginService.loggedInUserModel!.uid)
.collection("cartItems")
.doc(code)
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
SellerNameService isSellerName =
Provider.of<SellerNameService>(context, listen: false);
var sellerName = isSellerName.isSellerName;
final snapshotData = snapshot.data;
if (snapshot.hasData) {
return Text(
snapshot.data![sellerName].toStringAsFixed(2),
textAlign: TextAlign.center,
);
} else {
return Text('No Datos');
}
});
}
}
添加到购物车:
void add(BuildContext context, CartItem item) async {
_items.add(item);
int indexOfSeller = await getValue() ?? "";
LoginService loginService =
Provider.of<LoginService>(context, listen: false);
Map<String, dynamic> cartMap = Map();
var price = item.subCategory!.price as double;
var isSelectedPrice = item.isSelectedPrice;
var sellerName = item.subCategory!.parts[indexOfSeller].name;
var codeTest = item.subCategory!.code!;
var amount = item.subCategory!.amount;
_items.forEach((CartItem item) {
cartMap[item.subCategory!.code!] =
(item.subCategory as SubCategory).amount;
//
cartMap[item.subCategory!.parts[indexOfSeller].name] =
(item.subCategory!).parts[indexOfSeller].price;
});
_instance = FirebaseFirestore.instance;
_instance!
.collection('shoppers')
.doc(loginService.loggedInUserModel!.uid)
.collection("cartItems")
.doc(codeTest)
.set({codeTest: amount, sellerName: price},
SetOptions(merge: false)).then((codeTest) {
notifyListeners();
});
}
2条答案
按热度按时间wztqucjr1#
8zzbczxx2#
将StreamBuilder Package 在扩展的小部件中