I am new to using RiverPod. previously I used Provider for state management.
in case of provider I could use a provider outside widget tree to get value using syntax
Provider.of<MyModel>(context,listen:true).someFunction();
how do I do the same in RiverPod? for now I am using Consumer Builder and Consumer Widget. I was wondering if there's a way to call a riverpod provider without using Consumer.
You can easily call riverpord
provider without using Consumer
. Please check out my below extension which is very helpful to call the provider based on context where it will work for the read function without any consumer or consumer widget.
extension Context on BuildContext {
// Custom call a provider for reading method only
// It will be helpful for us for calling the read function
// without Consumer,ConsumerWidget or ConsumerStatefulWidget
// Incase if you face any issue using this then please wrap your widget
// with consumer and then call your provider
T read<T>(ProviderBase<T> provider) {
return ProviderScope.containerOf(this, listen: false).read(provider);
}
}
After then under the build method, you must call context.read(yourProviderName)