0

I have a usecase as follow: a screen with initial filter object, this screen is used multiple times with different filter configuration, what I need to achieve is to pass an initial different filter object for each instance from this screen to riverpod notifier provider then I can change the state of this filter using the provider, how I can do that?

I have tried to pass a filter object to the build method but this didn't work.

Amr
  • 148
  • 7

1 Answers1

1

You need to decide whether you want only one filter for that provider at a time, or whether you want many providers with their own filters. If the latter, pass something about the filter in as a family key. If the first, then have the build method of the provider consume (ref.watch) another provider that holds the filter. That filter-holder can then be updated via mutate methods on its notifier.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • Thanks, to be more clear: in StateNotifier I can pass an initial value, then I can mutate the state without being affected by this initial value as it's not recomputed again, but (as far as I know) in new Notifier/AsyncNotifier classes, the initial value is determined by the build method... how I can pass an initial value to the Notifier class from outside without using it as a family like what I can do in StateNotifier. Thanks so much. – Amr Jun 05 '23 at 23:01
  • 1
    It's either gonna derive from something the family key is relating to, or it's gonna be an internal default data value, or it's gonna be the value of a provider via ref.watch. That's how you select an initial value. – Randal Schwartz Jun 06 '23 at 02:33