-
-
Notifications
You must be signed in to change notification settings - Fork 908
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
Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt, when chaining providers #3354
Comments
After thinking about the issue, I now understand the reason behind the error messages. |
You're correct, this is indeed quite unintuitive. I need to think about the desirable behaviour here. A simpler reproduction is: @riverpod
class MyViewModel extends _$MyViewModel {
@override
String build() => ref.watch(myStateProvider);
void crash() {
ref.read(myStateProvider.notifier).state = 'Foo';
ref.read(myStateProvider);
}
} Although this is consistent with how the assert behaves, that's wayy too inconvenient. I'll change this. |
This is probably going to stay like this for some time. For now, it seems to be a bit niche. If this gets a bunch of 👍 I'll consider it more. |
So we can not read state immediately after change the state. Sometimes I got this exception but didn't know what happened. |
Indeed. As I said I'll likely change this, but at the moment I need to think about how. And it doesn't seem like a very active issue, so we'll see |
Here as well, hopefully it can be resolved |
@dumikaiya Please thumb up the issue when you support it! |
please thumbs up this issue guys |
Also does anyone know a workaround this? Currently I just wrap the |
I currently use something like this, is it okay? or can it cause unexpected errors? Thanks @riverpod
class MyViewModel extends _$MyViewModel {
@override
String build() => ref.watch(myStateProvider);
void crash() {
try{
ref.read(myStateProvider.notifier).state = 'Foo';
ref.read(myStateProvider); // usually ref.read(otherNotifier).someMethod();
} catch (e) {
if (e is AssertionError) {
if (e.toString().contains(
'Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt')) {
} else {
rethrow;
}
} else {
rethrow;
}
}
}
} |
I'll tackle this in a few weeks. I do plan in fixing this before the official 3.0 release. |
Is there a way to disable the assertion until that's done? I can't think of a way to refactor the application to avoid reading from a provider after a set but before a rebuild. |
No. To begin with, this isn't a problem in the stable version. |
Describe the bug
This example has got two providers, one state provider, which persists the entered text, and a view model provider, which forwards the entered text from the state provider & also forwards the input event to the state provider. The example is very stripped down, and obviously you could do the same thing with only one provider, without any issue, however the issue appeared in a more complex real world situation where two providers were necessary.
To Reproduce
The exception disappears when you move the read the notifier inside the
onChanged
callback or stop watching the view model provider, as indicated in the comments.Expected behavior
No exception should be thrown.
The text was updated successfully, but these errors were encountered: