Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually dispose provider #1295

Closed
Wishmater opened this issue Mar 21, 2022 · 10 comments
Closed

Manually dispose provider #1295

Wishmater opened this issue Mar 21, 2022 · 10 comments
Labels
enhancement New feature or request needs triage

Comments

@Wishmater
Copy link

Is your feature request related to a problem? Please describe.

In my app, I have a lot of providers that make relatively expensive API calls to compute their state. The app has many different pages, and the first time each page is entered, the API call is made to get the data and then kept so it doesn't have to be remade next time the user enters the same page (the providers are NOT auto-dispose).

There are some events that can happen that require me to remake all calls, to update the info shown. The problem is that if I do ref.refresh() for all providers when this event happens, all API calls for the providers refreshed will be remade simultaneously, causing each to take way longer. Ideally, all current state of the providers should be invalidated, and then remade when the user navigates to each of the pages again.

Describe the solution you'd like

A way to call ref.dispose() on the providers that have invalidated state, so the state is only recomputed when needed, not immediately.

Describe alternatives you've considered

Somehow getting the container and manually disposing the providers. This would be very hacky and I don't really know how to do it.

@Wishmater Wishmater added enhancement New feature or request needs triage labels Mar 21, 2022
@rrousselGit
Copy link
Owner

Is ref.invalidate what you're looking for?

@Wishmater
Copy link
Author

I am sorry, I can't seem to find a method ref.invalidate. I'm using flutter_riverpod 1.0.3. Am I missing something obvious here?
Such a method would be what I am looking for, yes.

@Wishmater
Copy link
Author

Wishmater commented Mar 22, 2022

Oh, now I see you have it planned for 2.0.0. It looks great, this would solve my issue perfectly, I'm looking forward to it.
I know this can be hard, but do you have an estimate of when it will be released?

@rrousselGit
Copy link
Owner

The feature is already available in a dev release, so you can use it today.

@Wishmater
Copy link
Author

I am sorry to keep bothering you but now I am using ver 2.0.0-dev.5 and invalidate doesn't behave the way I would expect. The documentation says:
/// As opposed to [refresh], the refresh is not immediate and is instead
/// delayed to the next read or next frame.

"next read or next frame" is not clear to me. I would expect that if the provider doesn't have any active listeners, it will not be refreshed on the next frame, but wait until a new listener calls read or watch. Instead I am getting exactly the same behavior as refresh: all providers are recomputed immediately.

Am I correct to assume that the intended behavior is for the provider to wait for an active listener before it refreshes?

Also, is there a way to debug to see if a provider currently has any active listeners?

@rrousselGit
Copy link
Owner

I would expect that if the provider doesn't have any active listeners, it will not be refreshed on the next frame but wait until a new listener calls read or watch

That's the case

Chances are you do have an active listener

Also, is there a way to debug to see if a provider currently has any active listeners?

Not really. There's a devtool planned though

@rrousselGit
Copy link
Owner

Closing as ref.invalidate should take care of this.

@arreshashikant
Copy link

How can we invalidate a provider in a StatefulWidget's State.dispose?

@gaganyadav80
Copy link

@arreshashikant You should not use ref in dispose method as explained here #2538. You can also find alternate ways of doing this in this link.

@Rohit19060
Copy link

May I know how to clear an async provider or we should create a method in all the async providers and update the state to a blank or null sate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

5 participants