riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

If `provider` is in `dependencies`, make it optional to add `provider.notifier`

Open haoz33 opened this issue 2 years ago • 1 comments

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,
  ],
);

haoz33 avatar Nov 08 '21 00:11 haoz33

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

rrousselGit avatar Nov 09 '21 18:11 rrousselGit

This was fixed in a previous refactoring. So I'll close this

rrousselGit avatar Aug 16 '22 19:08 rrousselGit