I was creating an application where, I need to call a function, which will take Ref as input (as it can be called by the providers), but I also need to call it using WidgetRef, while listening to a StateNotifier Provider.
final firstProvider = ... //Initialization
final someProvider = Provider<String>((ref) {
performOperation(ref);
return '';
});
class MyWidget extends ConsumerWidget {
...
Widget build(BuildContext context, WidgetRef ref) {
ref.listen(firstProvider, (pref, next) => performOperation(ref));
return ...;
}
}
Where the performOperation function might be like this:-
void performOperation(Ref ref) {
ref.read(...);
ref.read(...);
...
}