-
-
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
NotifierProvider misses ref #2460
Comments
This is intentional. It doesn't make sense for two reasons:
|
That's fine but it should be documented. Neither the documentation of When I discovered
Can't follow you here. The syntax of the |
It already is documented that the Notifier instance is preserved across ref.watch updates. Cf the dartdoc of the
https://pub.dev/documentation/riverpod/latest/riverpod/AsyncNotifier/build.html |
The syntax is: @riverpod
class MyNotifier extends _$MyNotifier {
@override
int build() {
<use ref here>
}
} There's no place for using |
The documentation states:
It explicitly states what happens when a dependency of I want to call
final notifierCount = NotifierProvider<Counter, int>((ref) {
// Changes of featureFlag causes the lambda to run again
final flag = ref.watch(featureFlag);
if (flag) {
return AlwaysFive();
}
return CounterImpl();
}); class Counter extends Notifier<int> {
@override
int build() {
// doesn't call watch
return 0;
}
void increment() {
state++;
}
}
class AlwaysFive extends Notifier<int> {
@override
int build() {
// doesn't call watch
return 5;
}
} Maybe there are not two different kind of dependencies in the current implementation - one for the |
|
It makes sense but at the same time that would mean we'd effectively create two separate providers. This can get a bit weird, as the notifier vs the "build" method may have different dependencies/life-cycles/... |
Hello @rrousselGit , sorry for late discussion but i want make sure about using So i have simple MVVM architecture and have injection file to connected all provider, current implementation work out the box using But i notice, This some example about my code : injection.dart
exampleProvider.dart
|
Work tracked in #2630 |
I noticed an API difference between
Provider
,ChangeNotifierProvider
andNotifierProvider
.NotifierProvider
doesn't takeref
as argument inNotifierProvider.creator
andNotifierProvider.overrideWith
.All I really want is to define a
NotifierProvider<NotifierT, T>
and swap the actual implementation of that provider at runtime, making all Providers that depend onNotifierT
andT
rebuild.And that already works for
Provider<T>
!It works with
ChangeNotifierProvider<T>
!But it does not work for
NotifierProvider<NotifierT, T>
becauseNotifierProvider.creator
doesn't passref
as argument. I do not understand the rational between the APIs being different.Same for
overrideWith
which takesref
as argument forProvider
andChangeNotifierProvider
but not forNotifierProvider
.Is this an oversight, intentional or just a limitation of the current implementation?
This is a follow-up to #2458
The text was updated successfully, but these errors were encountered: