I am new to Flutter, I am trying to fetch data from firestore collection. I am using FutureBuilder with Provider, but it shows null error on initial & runs perfectly after restarting the app.
Here is the code to fetch the data:
FutureBuilder<QuerySnapshot>(
future: FirebaseFirestore.instance
.collection(collec.collectionProducts)
.get(),
builder: (context, snapshot) {
return Consumer<CartItemCounter>(
builder: (BuildContext context, CartItemCounter cart, _) {
if (cart.checkProductAddedToCart(model.productId)) {
return Row(....);
code for ui
(here i have changed the collection name)
How can i solve it. I have tried every solution available on online. Thank you
Where is the error coming from?
According to this, the issue is coming from your Consumer<CartItemCounter>
.
What does the error mean?
This error, The method 'x' was called on null.
, means that the class where this x
function is written, that class is null.
What is the reason for this error in my code?
Your Consumer<CartItemCounter>
provides an instance of CartItemCounter
by the name cart
. Then, you call function checkProductAddedToCart
.
The error message is telling you that cart
is null which is being given to you by the Consumer
widget. This means that Consumer probably also did not find that Provider. I expect that there should be an error log by Provider
, telling you that CartItemCounter was not found.
Possible Solution
Try 'providing' CartItemCounter.
How do you do that? Go to the top of your app's widget tree to MaterialApp
, wrap it inside a MultiProvider
or ChangeNotifierProvider
and then pass your new CartItemCounter as value
.
Read Official Provider Docs for more info on how to expose a provider.
You need to upload your complete log, so that I can further help you.