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

Am I allowed to ref.watch in a FutureProvider across async gap? #1603

Closed
fzyzcjy opened this issue Sep 4, 2022 · 6 comments
Closed

Am I allowed to ref.watch in a FutureProvider across async gap? #1603

fzyzcjy opened this issue Sep 4, 2022 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@fzyzcjy
Copy link
Contributor

fzyzcjy commented Sep 4, 2022

For example,

final myProvider = FutureProvider<Something>((ref) async {
  final a = ref.watch(aProvider);
  final b = await fetchNetworkData(a);
  final c = ref.watch(cProvider); // <---- NOTE this line
  return ...;
});

If it is supported, maybe the doc can mention it a little bit, because I am familiar with MobX which does not support this. Given popularity of MobX, I guess many people will have the same confusion as me, so doc is helpful.

If it is not supported, then this issue is a feature request :)

@fzyzcjy fzyzcjy added enhancement New feature or request needs triage labels Sep 4, 2022
@fzyzcjy fzyzcjy changed the title Can I ref.watch in a FutureProvider across async gap? Am I allowed to ref.watch in a FutureProvider across async gap? Sep 4, 2022
@TimWhiting
Copy link
Collaborator

TimWhiting commented Sep 4, 2022

Yes, it is supported, note that the provider will only rebuild when aProvider changes until the await completes successfully, at which point cProvider is watched, after which the provider will rebuild whenever cProvider changes as well.

@TimWhiting TimWhiting added documentation Improvements or additions to documentation and removed enhancement New feature or request needs triage labels Sep 4, 2022
@fzyzcjy
Copy link
Contributor Author

fzyzcjy commented Sep 4, 2022

Thank you!

@rrousselGit
Copy link
Owner

There are no constraints on when to call ref.watch/ref.read, only good/bad practices.

For example, you can call ref.read inside build. It's considered a bad idea, but you can. Same goes for ref.watch inside a button's onPressed.

@levi956
Copy link

levi956 commented Sep 9, 2022

But could those bad practices cause breakage/exceptions or unnecessary widget rebuilds?

@rrousselGit
Copy link
Owner

rrousselGit commented Sep 10, 2022 via email

@iLoveDocs
Copy link
Sponsor

@rrousselGit

As you mentioned,

if the watched provider changes, the widget/provider will rebuild.

But it doesn't seem to be working as expected. Could you please take a look at this question on StackOverflow?

@rrousselGit rrousselGit self-assigned this May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants