You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am calling a StateNotifier's function in initState to fetch something, and sometimes this is fetched from a cache, in which case it's so fast that by the time the ref.listen is called in the build method, that provider's state has already changed and the callback is not called.
The only way I've got round this so far is to do a ref.listen and also a ref.read on the same provider, calling the callback straight after it if the state is not null (which is the initial state).
But I know that ref.read shouldn't be used inside the build method.
Should I instead just be using ref.watch (and no ref.listen) and then calling the callback afterwards if that state isn't null? I wanted to avoid unnecessary rebuilds though (which is the point of ref.listen)
Another issue with having the callback directly in the build method after ref.watch or ref.read is that it will get called whenever the widget is rebuilt
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am calling a StateNotifier's function in initState to fetch something, and sometimes this is fetched from a cache, in which case it's so fast that by the time the ref.listen is called in the build method, that provider's state has already changed and the callback is not called.
The only way I've got round this so far is to do a ref.listen and also a ref.read on the same provider, calling the callback straight after it if the state is not null (which is the initial state).
But I know that ref.read shouldn't be used inside the build method.
Should I instead just be using ref.watch (and no ref.listen) and then calling the callback afterwards if that state isn't null? I wanted to avoid unnecessary rebuilds though (which is the point of ref.listen)
Another issue with having the callback directly in the build method after ref.watch or ref.read is that it will get called whenever the widget is rebuilt
Beta Was this translation helpful? Give feedback.
All reactions