-
-
Notifications
You must be signed in to change notification settings - Fork 956
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
Add an asynchronous StateNotifier #784
Comments
Closing since this is supported directly by So creating a |
@rrousselGit I think maybe I'm missing something here, I see how to use AsyncValue to wrap state to a StateNotifier, but what if I have a
How can I do the above as I'd need to make the |
Actually I've ended up just doing this:
and making my |
I agree with @maks here, and my use case is exactly like the above |
@rrousselGit can you please point us where those extension methods are located? preferably an example usage as well. Looking for the extension, I searched repo both your repos for I need something like |
Actually, I just found a possible answer hidden in a changelog: https://github.com/rrousselGit/riverpod/blob/master/packages/riverpod/CHANGELOG.md#general-changes
class MyAsyncStateNotifier extends StateNotifier<AsyncValue<MyState>> {
MyAsyncStateNotifier(): super(AsyncValue.loading()) {
// TODO fetch some data and update the state when it is obtained
}
}
final myAsyncStateNotifierProvider = StateNotifierProvider<MyAsyncStateNotifier, AsyncValue<MyState>>((ref) {
return MyAsyncStateNotifier();
});
final someFutureProvider = FutureProvider((ref) async {
MyState myState = await ref.watch(myAsyncStateNotifierProvider.future);
}); However, unfortunately I cannot find a way to prematurely enforce an final myAsyncStateNotifierProvider = StateNotifierProvider<MyAsyncStateNotifier, AsyncValue<MyState>>((ref) {
// 1. Fetch/Calculate Result. Some asynchronous Future/AsyncValue goes here
// 2. Result not ready. I want to return a null `MyAsyncStateNotifier?` or AsyncLoading here but can't!
// 3. Check if result ready. Future/AsyncValue finished their work.
// 4. Result ready. Return `MyAsyncStateNotifier`
return MyAsyncStateNotifier();
}); I cannot seem to achieve the above flow |
Did this feature go away? I'm on
|
Yes, it never reached the stable release Use AsyncNotifier instead |
Is your feature request related to a problem? Please describe.
It's related to this Issue. #57, #329
#57 (comment)
This solution is good. However, other providers do not allow synchronous calls.
Describe the solution you'd like
I made the draft here.
https://github.com/ryicoh/future_state_notifier/blob/85b65cc7c2f96887dcb1442df5945831464623a0/lib/future_state_notifier.dart#L6
Example of
Counter
https://github.com/ryicoh/future_state_notifier/blob/85b65cc7c2f96887dcb1442df5945831464623a0/test/future_state_notifier_test.dart#L33
If this provider is to be used within another provider:
https://github.com/ryicoh/future_state_notifier/blob/85b65cc7c2f96887dcb1442df5945831464623a0/lib/future_state_notifier.dart#L33
If this provider is to be used within a flutter widget:
https://github.com/ryicoh/future_state_notifier/blob/85b65cc7c2f96887dcb1442df5945831464623a0/lib/future_state_notifier.dart#L38
I know that the combination of "StateNotifier" and "FutureNotifier" does not result in "Async.loading()" when the state is updated.
https://github.com/ryicoh/future_state_notifier/blob/85b65cc7c2f96887dcb1442df5945831464623a0/test/future_state_notifier_test.dart#L87
The text was updated successfully, but these errors were encountered: