Replies: 6 comments
-
|
I have the same question. I am currently passing the provider from A to B, C and D by constructor parameter. |
Beta Was this translation helpful? Give feedback.
-
|
An alternative would be storing the family parameter with a provider. @riverpod
Future<Foo> foo(FooRef ref, {required int id}) async {
/* fetch foo with id */
return ...
}We could do @riverpod
class SelectedFooId extends _$SelectedFooId {
@override
int build() => 1;
void update(int id) => state = id;
}
@riverpod
Future<Foo> foo(FooRef ref) async {
final selectedFooId = ref.watch(selectedFooIdProvider);
/* fetch foo with id */
return ...
}The documentation here explained combining providers with the |
Beta Was this translation helpful? Give feedback.
-
|
This is a common situation. I want to solve it by passing provider as a parameter to child widget. But I found that AsyncNotifierProvider that with some arguments is not based AsyncNotifierProvider . I did not get the correct base class. I set the parameter type to AutoDisposeAsyncNotifierProvider is OK to AsyncNotifierProvider that without any arguments. But not work when I add some arguments to the provider. The error is @rrousselGit Any suggestions for us? |
Beta Was this translation helpful? Give feedback.
-
|
Can you show some sample code ? like how do you use B, C and D under A? |
Beta Was this translation helpful? Give feedback.
-
|
i was also stuck trying to do this until i realised that this isnt how families work, they kind of do the opposite, families allow for multiple instances of the same provider, the parameters you pass are more like identifiers final appleProvider = ref.read(myProvider('apple')) so calling ref.read(myProvider('apple')) later in the tree will get me the same provider but ref.read(myProvider('strawberry')) would be a new instance, i refactored mine to be a normal change notifier no params and a public init method which i think @rrousselGit argues against, but all my tests pass 😂 and it only interacts with one widget so if you want to reuse it you reuse the widget 🤷 |
Beta Was this translation helpful? Give feedback.
-
|
As @martipello say:
I had to change my mind |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sorry for my bad English.
Assume, I have screen
AincludeB,CandDwidgets.Widget tree:
A (parent)
--- B (child)
--- C (child)
--- D (child)
And I have a provider (with family):
I would like initialize
myAsyncNotifierin screenA.B,CandDjust need using it.In
A:My problem is how to use myAsyncNotifierProvider without passing data in
B,CandD? Because all of them just using, not need initialize.Beta Was this translation helpful? Give feedback.
All reactions