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
Describe the bug
Once a StateNotifierProvider was created (e.g. via ref.watch(provider), it cannot be invalidated with ref.invalidate(provider), even though the widget that called ref.watch was disposed long ago and nothing else is listening.
I further analyzed the problem and figured out that the StateNotifierProvider is the only Provider calling ref.watch in their create() in base.dart:
This call to ref.watch adds the _NotifierProvider, inialized during StateNotifierProvider's inialization, to the ProviderElement's _depenents once the StateNotifierProvider was created.
Later upon ref.invalidate, the StateNotifierProvider is refreshed since hasListeners is true in scheduler.dart:
void_performRefresh() {
/// No need to traverse entries from top to bottom, because refreshing a /// child will automatically refresh its parent when it will try to read itfor (var i =0; i < _stateToRefresh.length; i++) {
final element = _stateToRefresh[i];
if (element.hasListeners) element.flush();
}
}
To Reproduce
I created a minimal app which reproduces the bug. Just note the reaccuring state notifier initialized when clicking on Invalidate Providers.
Expected behavior
Expected behavior is that the StateNotifierProvider will not be refreshed as no listener (in the client code) is attached. Therefore, the ref.watch called in the create method must be disposed while the StateNotifierProvider is disposed.
I already created an issue (#1704) which did not describe the issue that clearly. Therefore, I closed that one to describe the bug in this issue in more detail.
The text was updated successfully, but these errors were encountered:
Describe the bug
Once a StateNotifierProvider was created (e.g. via
ref.watch(provider)
, it cannot be invalidated withref.invalidate(provider)
, even though the widget that calledref.watch
was disposed long ago and nothing else is listening.I further analyzed the problem and figured out that the
StateNotifierProvider
is the only Provider callingref.watch
in theircreate()
in base.dart:This call to
ref.watch
adds the_NotifierProvider
, inialized duringStateNotifierProvider
's inialization, to theProviderElement
's_depenents
once theStateNotifierProvider
was created.Later upon
ref.invalidate
, theStateNotifierProvider
is refreshed sincehasListeners
istrue
in scheduler.dart:To Reproduce
I created a minimal app which reproduces the bug. Just note the reaccuring
state notifier initialized
when clicking onInvalidate Providers
.Expected behavior
Expected behavior is that the
StateNotifierProvider
will not be refreshed as no listener (in the client code) is attached. Therefore, theref.watch
called in thecreate
method must be disposed while theStateNotifierProvider
is disposed.I already created an issue (#1704) which did not describe the issue that clearly. Therefore, I closed that one to describe the bug in this issue in more detail.
The text was updated successfully, but these errors were encountered: