Closed stargazing-dino closed 5 years ago
That exception is happening because you're misusing ChangeNotifierProvider
.
You don't want to use the builder constructor when you already have an instance of ChangeNotifier
. The default constructor will dispose the ChangeNotifier
when ChangeNotifierProvider()
is unmounted.
Instead, you probably want ChangeNotifierProvider.value
:
ChangeNotifierProvider.value(
value: products[i],
child: ProductItem(),
);
Thank you! Works just fine now
@rrousselGit thanks a lot that actually helped me as well fixing my crashes.
Also in the mentioned course the recommended approach is to use the .value()
constructor.
From the final code of the related course section:
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: products[i],
child: ProductItem(),
),
But it's an important info of course, that and why the other approach can't work.
Thank you now works fine foe me.
I scanned through nearly 100 old issues looking for the purpose of the .value
constructors. Now I think I understand: when you need to manage disposal yourself.
I scanned through nearly 100 old issues looking for the purpose of the
.value
constructors. Now I think I understand: when you need to manage disposal yourself.
That's only a side effect
The main purpose is: when the provider is not the owner of the value to provides.
And since the provider is not the object owner, it doesn't make sense for the provider to dispose of the value. Hence why you need to dispose it yourself in such situation
The main purpose is: when the provider is not the owner of the value to provides.
Yes, I would have understood an "ownership" explanation of the difference. Ownership to me means controlling the lifespan of an object.
I'm mentoring someone who is learning Java (and some software design) as she creates unit tests for a project of mine. She does not yet understand what "ownership" means, so I suspect this explanation mainly speaks to experienced devs.
And since the provider is not the object owner, it doesn't make sense for the provider to dispose of the value. Hence why you need to dispose it yourself in such situation
Yes, it's a consequence of ownership. But in the context of provider
, this seems to be the only consequence that matters: who manages the lifetime of the object.
May I ask how to properly dispose a ChangeNotifier
, especially in the ListView.builder
as a child? Since It is not a good idea to dispose inside the widget which is not the owner of the object.
Hi!
TL;DR If a
ChangeNotifierProvider
with nestedListenableProvider
(s) is disposed, will it also dispose theListenableProvider
(s)?I recently found a cool way to use Providers by Acedemind I'd never seen before. Here is an example where he has a
Products
notifier that handles a list of products and aProduct
notifier that itself is a single instance of a product:And then to show the items, you'd say something like this:
And so if you tap on the tile and hit
product.toggleFavoriteStatus()
it won't actually notify everything that's this one notifier has changed, just that one tile and the heart will change color.I really like this idea of nested providers but there was an issue when applying it to a
ListView.builder
when the list scrolls. If your list is long enough theChangeNotifierProvider
on the product will dispose of an item that is no longer visible (when probably the ListState is still referencing it) and you'll get:Changing the
ChangeNotifierProvider
to aListenableProvider
will fix that issue, but I'm worried about memory leaks now as nothing is explicitly disposing of the product.