riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

What is the difference between Ref and WidgetRef?

Open 0xpussycat opened this issue 2 years ago • 1 comments

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?

0xpussycat avatar Mar 16 '22 00:03 0xpussycat

WidgetRef doesn't add features, it removes some.

There's no life-cycle hooks such as onDispose/keepAlive/onCancel/...

rrousselGit avatar Mar 18 '22 10:03 rrousselGit

In Riverpod 2.0 migration due to WidgetRef & Ref issue it sucks. 😢

pranavo72bex avatar Nov 11 '22 07:11 pranavo72bex

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.

FaFre avatar Apr 30 '23 16:04 FaFre

You're still not meant to do something like this.

Using WidgetRef means you're effectively putting your logic in the UI.

rrousselGit avatar Apr 30 '23 21:04 rrousselGit

Planned to add this to the upcoming FAQ page https://github.com/rrousselGit/riverpod/issues/2623

rrousselGit avatar Jun 20 '23 15:06 rrousselGit