我是颤振的新手,我正在尝试将产品添加到购物车,但是当我单击添加按钮时,它显示该collection
方法在 null 上调用了我用来将产品添加到用户购物车的函数:
void checkItemInCart(String shortInfoAsID, BuildContext context) {
Constants.sharedPreferences
.getStringList(Constants.userCartList)
.contains(shortInfoAsID)
? Fluttertoast.showToast(msg: "Item is already in Cart.")
: addItemToCart(shortInfoAsID, context);
}
addItemToCart(String shortInfoAsID, BuildContext context) {
List tempCartList =
Constants.sharedPreferences.getStringList(Constants.userCartList);
tempCartList.add(shortInfoAsID);
Constants.firestore
.collection(Constants.collectionUser)
.document(Constants.sharedPreferences.getString(Constants.userUID))
.updateData({
Constants.userCartList: tempCartList,
}).then((v) {
Fluttertoast.showToast(msg: "Item Added to Cart Successfully.");
Constants.sharedPreferences
.setStringList(Constants.userCartList, tempCartList);
Provider.of<CartItemCounter>(context, listen: false).displayResult();
});
}
我也有常量文件,它是:
class Constants {
static const String appName = 'App name';
static SharedPreferences sharedPreferences;
static FirebaseUser user;
static FirebaseAuth auth;
static Firestore firestore;
static String collectionUser = "users";
static String collectionOrders = "orders";
static String userCartList = 'userCart';
static String subCollectionAddress = 'userAddress';
static final String userName = 'name';
static final String userEmail = 'email';
static final String userPhotoUrl = 'photoUrl';
static final String userUID = 'uid';
static final String userAvatarUrl = 'url';
static final String addressID = 'addressID';
static final String totalAmount = 'totalAmount';
static final String productID = 'productIDs';
static final String paymentDetails = 'paymentDetails';
static final String orderTime = 'orderTime';
static final String isSuccess = 'isSuccess';
}
我得到的错误是:
════════ Exception caught by gesture ═══════════════════════════════════════════
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:shipit/Store/storehome.dart:371
#2 checkItemInCart
package:shipit/Store/storehome.dart:362
我希望你能帮助我提前谢谢你感谢它。