riverpod
riverpod copied to clipboard
If `provider` is in `dependencies`, make it optional to add `provider.notifier`
Describe the bug
I have recently updated to version 1.0.0 and encountered some issue with using the dependencies parameter when creating provider, I'm not sure if this is a bug or intended but it does seems a bit anti-intuition to have explicitly pass both stateNotifierProvider
and stateNotifierProvider.notifier
into the dependencies list. It will be great if any clarification that can be provided. Thanks in advance!
To Reproduce
Consider the code below where the MainNotifier
makes use of the DependentNotifier
's state and function. In order to make the code work and not throw an exception when calling the doSomething
function inside the MainNotifier
class, both dependentProvider
and dependentProvider.notifier
are required in the dependencies list for the provider.
class DependentNotifier extends StateNotifier<String> {
DependentNotifier() : super('dependent');
String returnSome() {
return 'some';
}
}
class MainNotifier extends StateNotifier<String> {
MainNotifier(this.value, this._ref) : super('main');
final Ref _ref;
final String value;
void doSomething() {
final a = _ref.read(dependentNotifier);
final b = _ref.read(dependentNotifier.notifier).returnSome();
state = state + value + a + b;
}
}
final valueProvider = Provider<String>((ref) => throw UnimplementedError());
final dependentProvider = StateNotifierProvider<DependentNotifier, String>(
(ref) => DependentNotifier(),
);
final mainProvider = StateNotifierProvider<MainNotifier, String>(
(ref) => MainNotifier(ref.watch(valueProvider), ref),
dependencies: [
valueProvider,
dependentProvider,
dependentProvider.notifier,
],
);
Expected behavior
Only need to pass in the dependentProvider
.
final mainProvider = StateNotifierProvider<MainNotifier, String>(
(ref) => MainNotifier(ref.watch(valueProvider), ref),
dependencies: [
valueProvider,
dependentProvider,
],
);
That looks like an edge-case I didn't think of.
I'll see if I can change that, but for now yes, you'll need to add boths to your dependencies
list
This was fixed in a previous refactoring. So I'll close this