Is there a way to access an instance of a provided ".family
" change notifier (which has already been instantiated, passing the right parameters) without passing the parameters again?
IN PROVIDER
When you create a provider of a ChangeNotifier (which requires parameters), you can get the same change notifier it's providing with Provider.of<ChangeNotif>(context)
;
class ChangeNotif extends ChangeNotifier {
final String id;
const ChangeNotif(this.id);
}
ChangeNotifierProvider(create: (_) => ChangeNotif("dash"));
Once the provider has been created with its right params, you can get whatever it's providing anywhere down that widget tree without any syntax like Provider.of<ChangeNotif("dash")>(context)
but rather Provider.of<ChangeNotif>(context)
.
IN RIVERPOD
Since you have to pass the parameters to the provider when getting an instance of it, I've had to assign the provider to a variable in order to pass it down to its children which need the change notifier the provider is providing.
final changeNotifProvider = ChangeNotifierProvider.family<ChangeNotif, String>((ref, id) => ChangeNotif(id));
class A extends HookWidget {
build() {
final _changeNotifProvider = changeNotifProvider("dash");
final _changeNotif = useProvider(_changeNotifProvider);
return Column(
children: [
B(),
c(_changeNotifProvider),
]
);
}
}
Is there a way to get the instantiated _changeNotif
without passing it as a parameter to a child widget? Is there any way to get the same instance of _changeNotif in another widget that isn't a child of A (like using Provider.of<ChangeNotif>(context)
in Provider without having to pass new parameters)?