我的项目出错 - 在 null 上调用了'collection'方法



好,

我的应用程序有一个问题,因为它在按下添加到购物车按钮时会产生错误,它会产生错误并且没有添加到firebase中,我已经检查了变量的名称是否正确,但我找不到如何解决这个错误,我感谢你能给我的所有帮助来解决这个错误!

void checkItemInCart(String shortInfoAsID, BuildContext context)
{
EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsID)
? Fluttertoast.showToast(msg: "El artículo ya existe en el carrito")
: addItemToCart(shortInfoAsID, context);
}
addItemToCart(String shortInfoAsID, BuildContext context) {
List tempCartList = EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList);
tempCartList.add(shortInfoAsID);
EcommerceApp.firestore.collection(EcommerceApp.collectionUser)
.document(EcommerceApp.sharedPreferences.getString(EcommerceApp.userUID))
.updateData({
EcommerceApp.userCartList: tempCartList,
}).then((v){
Fluttertoast.showToast(msg: "Artículo añadido al carrito");
EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList, tempCartList);
Provider.of<CartItemCounter>(context, listen: false).displayResult();
});
}
The following NoSuchMethodError was thrown while handling a gesture:
The method 'collection' was called on null.
Receiver: null
Tried calling: collection("users")
When the exception was thrown, this was the stack: 
0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
1      addItemToCart (package:e_shop/Store/storehome.dart:283:26)
2      checkItemInCart (package:e_shop/Store/storehome.dart:276:9)
3      sourceInfo.<anonymous closure> (package:e_shop/Store/storehome.dart:246:27)
4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#696c2
debugOwner: GestureDetector
state: ready
won arena
finalPosition: Offset(338.5, 338.3)
finalLocalPosition: Offset(32.5, 27.8)
button: 1
sent tap down
====================================================================================================

声明这个:

List tempCartList = List<>();  

然后这样做:

void checkItemInCart(String shortInfoAsID, BuildContext context)
{
EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsID)
? Fluttertoast.showToast(msg: "El artículo ya existe en el carrito")
: addItemToCart(shortInfoAsID, context);
}

addItemToCart(String shortInfoAsID, BuildContext context) {
tempCartList = EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList);
tempCartList.add(shortInfoAsID);

EcommerceApp.firestore.collection(EcommerceApp.collectionUser)
.document(EcommerceApp.sharedPreferences.getString(EcommerceApp.userUID))
.updateData({
EcommerceApp.userCartList: tempCartList,
}).then((v){
Fluttertoast.showToast(msg: "Artículo añadido al carrito");

EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList, tempCartList);

Provider.of<CartItemCounter>(context, listen: false).displayResult();
});
}

相关内容

最新更新