Replies: 2 comments 8 replies
-
To begin with, you shouldn't modify other states inside providers when a provider is initializing. You probably want to move that logic to final weatherProvider = FutureProvider((ref) async {
...
weather.changeUnitSystem(ref.watch(unitSystemProvider));
return weather;
}); |
Beta Was this translation helpful? Give feedback.
7 replies
-
I am not sure if I get your question correctly. Maybe you could listen to state changes in unitSystemProvider, like final weatherProvider = FutureProvider((ref) => null);
final weatherWithUnitSystemProvider = FutureProvider<??>((ref) async {
final weather = await ref.watch(weatherProvider.future);
ref.listen(
unitSystemProvider,
(_, newState) {
ref.state = weather.changeUnitSystem(newState);
},
fireImmediately: true,
);
return ??;
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This problem has been driving me insane for weeks, so I'd appreciate any help on it.
Use case: to simplify things a bit, I have a
FutureProvider.family<Weather>
, and another provider that uses that provider but changes the weather's unit system (metric/imperial) based on user preference. Something like:However, the problem here is that if
weatherProvider
is refreshed withref.refresh(weatherProvider)
and produces an error, and the unit system is changed, thenweatherWithUnitSystemProvider
will not correctly update the weather with the new unit system.In a nutshell, what I'm wondering is if there's a sane way to make
.future
set the error field inweatherWithUnitSystemProvider
'sAsyncData
state, but ifweatherProvider
has a previous value, then complete the future with that value instead of throwing the error. That way, the error is propagated toweatherWithUnitSystemProvider
, but the unit system still gets updated correctly.Beta Was this translation helpful? Give feedback.
All reactions