i'm newbie with flutter i'm trying to add product to cart but when i click to add button it shown that collection
method is called on null
the function i'm using to add the product to the user cart :
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();
});
}
Also i have the constants file which is :
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';
}
the error i'm getting is :
════════ 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
i hope you can help me thank you in advance appreciate it.
This happens because Constants.firestore
is null
. You need to set Constants.firestore
to contain an instance of Firestore
. For example, you could do this:
// in lib/main.dart
void main() {
WidgetsFlutterBinding.ensureInitialized(); // make sure plugins are initialized
Constants.firestore = Firestore.instance; // Constants.firestore is not null after this line
runApp(MyApp()); // launch the app
}
If you set your main()
method to look like this, Constants.firestore
will never be null when you try to use it in your widgets/state management.
You may also want to consider doing this for the other things in Constants
too (e.g. FirebaseAuth
, SharedPreferences
etc)