Flutter gives null error, but works after restart
Asked Answered
G

2

1

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(....);

Error error-image

code for futurebuilder widget code

code for ui ui widget code (here i have changed the collection name)

How can i solve it. I have tried every solution available on online. Thank you

Guildhall answered 23/12, 2020 at 5:46 Comment(7)
is a circularprogressindicator showing on the screen?Aureus
yes for a whileGuildhall
make sure the collection and the where condition is correct, what does snap.data.documents.length give you?Aureus
it is giving null. since the collection and condition is okayGuildhall
if it is giving null, then you are not getting anything, what does print(itemHolder) give you?Aureus
it displays the chosen category from previous screenGuildhall
Let us continue this discussion in chat.Aureus
M
1

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.

Marquisette answered 24/2, 2021 at 8:44 Comment(4)
ChangeNotifierProvider(create: (c) => CartItemCounter()), Yes i have already added Provider in main.dartGuildhall
Yes it worked. I was using, ChangeNotifierProvider(create: (c) => CartItemCounter()). When i added, ChangeNotifierProvider.value(value: CartItemCounter()), i started to get null error on length. In the end i added both. now it's working. Silly me this thing took 1 whole day. Thank you mate..Guildhall
Hey, it worked fine, but i didn't noticed one thing that when i restart the app, all the data stored in Map object gets deleted. How can i avoid it? or how can i store it?Guildhall
You will have to save the cart items to the device's local storage and reload from there when application starts. The most easiest & fastest way to do this is Hive plugin. Study that.Marquisette
R
0

Always check the result for the future builder, ie snapshot has data.

Place a switch condition or if-else bock depending on the status of the snapshot.

check the example from the official doc here

Radius answered 23/2, 2021 at 9:20 Comment(3)
Yes it has. Because the circular progress is not showing. And also it starts working after restarting the app.Guildhall
I checked with switch block, there is data available but still same error comingGuildhall
try wrapping your cart with an if condition to check if its null, before accessing the method checkProductAddedToCartRadius

© 2022 - 2024 — McMap. All rights reserved.