riverpod
riverpod copied to clipboard
What is the difference between Ref and WidgetRef?
The functionality wise looks the same.
Why can't we have just Ref for everything? Basically, what WidgetRef gives in addition to Ref that it can't do?
WidgetRef doesn't add features, it removes some.
There's no life-cycle hooks such as onDispose/keepAlive/onCancel/...
In Riverpod 2.0 migration due to WidgetRef & Ref issue it sucks. 😢
For my scenario I found following solution by using fpdart:
void propagateItemChanges(Either<WidgetRef, Ref> ref) {
final read = ref.fold((l) => l.read, (r) => r.read);
...
}
This comes in handy since WidgetRef
and Ref
are not sharing any common super class.
You're still not meant to do something like this.
Using WidgetRef means you're effectively putting your logic in the UI.
Planned to add this to the upcoming FAQ page https://github.com/rrousselGit/riverpod/issues/2623