Ryde
Ryde

Reputation: 714

Riverpod base class for WidgetRef and ProviderRef

function(
  WidgetRef ref, 
  // ProviderRef ref,
) {
  final a = ref.read(aProvider);
  // Some logic
}

What base class that can read WidgetRef and ProviderRef?

Upvotes: 1

Views: 3457

Answers (1)

Ryde
Ryde

Reputation: 714

Apparently it's been answered in here by the creator https://github.com/rrousselGit/riverpod/discussions/1152

However, I wondered why Ref and WidgetRef don't share a common interface?

That's voluntary. The fact that you need a common interface shows a flaw in your architecture

You most definitely did something wrong somewhere. You should never need to use WidgetRef outside of widgets

So, there is no base class or common interface. It's considered there is flaw in your code if you need WidgetRef outside of widgets.

[Edit] Btw there is a way to share a function that works with both ref. By setting it parameters with notifier class (to change state), or just set the object (to read).

function(
  ANotifierClass noti, 
  AClass a,
) {
  noti.callSomething();
  // Do something with a
}

called like

// ref can be WidgetRef or Ref
function(
  ref.read(aProvider.notifier),
  ref.read(aProvider),
);

Upvotes: 3

Related Questions