Calling a method when a provider is created

420 Views Asked by At

With the ChangeNotifierProvider widget in Provider, you can call a method, say onInit(), immediately when a provider is created with create like so

ChangeNotifierProvider(create: (_) => SomeChangeNotifier(context: context)..onInit();

What is the riverpod way of doing the same?

I tried

final exampleProvider = ChangeNotifierProvider.autoDispose<ExampleChangeNotifier>((ref) => ExampleChangeNotifier(providerRef: ref)..onInit());

thinking onInit() will be called the first time exampleProvider is read or watched in the build of a widget but it doesn't get called.

0

There are 0 best solutions below