Handling Provider Disposal of an AsyncNotifierProvider? #3568
-
So I started working with Riverpod and I want a AsyncNotifierProvider to call my server and return a set of reviews to work with. The thing is that I don't use the provider in all my pages and from what I read, if I don't read my provider in a page, it will dispose of itself. I also read that provider have a dispose method which can handle what happens to a provider when it is meant to be disposed. The Problem is that the documentation on AsyncNotifierProvider using build_runner is sparse so I'm not sure where to start. I tried to overwrite a dipose method but I can't find it, and I am not sure how to enable a dipose catcher in build runner.
Also please correct me if I am wrong about how riverpod works in terms of creation and diposal.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can find more info on this topic here: https://riverpod.dev/docs/concepts/provider_lifecycles TLDR; in your build() method, use ref.onDispose(). |
Beta Was this translation helpful? Give feedback.
-
Oh now I feel bad that it was so easy to find.
You give ref a callback through the onDipose function, and It does trigger when I leave the page watching the provider. |
Beta Was this translation helpful? Give feedback.
You can find more info on this topic here: https://riverpod.dev/docs/concepts/provider_lifecycles
TLDR; in your build() method, use ref.onDispose().