rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
6.28k stars 957 forks source link

add ListenableProvider #119

Closed funwithflutter closed 2 years ago

funwithflutter commented 4 years ago

From the examples/docs it's not clear what the equivalent is for ListenableProvider (from Provider): https://pub.dev/documentation/provider/latest/provider/ListenableProvider-class.html

Or ValueListenableProvider: https://pub.dev/documentation/provider/latest/provider/ValueListenableProvider-class.html

Basically, how can I use Riverpod to expose a regular Listenable or ValueListenable?

Also, what is ValueProvider (Riverpod) and when should that be used?

rrousselGit commented 4 years ago

What are you referring to by ValueProvider? There is no such class

There's no built-in Provider for listening to Listenable at the moment.

funwithflutter commented 4 years ago

Referring to this: https://github.com/rrousselGit/river_pod/blob/master/packages/riverpod/lib/src/framework/value_provider.dart

rrousselGit commented 4 years ago

It isn't exported and is an implementation detail

funwithflutter commented 4 years ago

Seems to be exported through hooks_riverpod. That's how I stumbled on it. I was looking for ValueListenableProvider, and found ValueProvider.

rrousselGit commented 4 years ago

That's a mistake. I will hide it

Omeiza17 commented 3 years ago

Hello @rrousselGit any updates on the status of this issue( ListenableProvider)

rrousselGit commented 3 years ago

No

X-Wei commented 2 years ago

Something like a ValueListenableProvider would be helpful to watch ValueListenable objects.

For example hive provides box.listenable() such that the local DB changes trigger a widget rebuild: https://docs.hivedb.dev/#/basics/hive_in_flutter?id=valuelistenable it would be nice if we can have providers watching the box.listenable() rather than using ValueListenableBuilder

iamarnas commented 2 years ago

If someone want provide Listenable value and use it with Riverpod use ChangeNotifierProvider and convert it to Listenable:

/*...*/
listenableValue: ref.watch(counterProvider.notifier), 
/*...*/
/// Provide `Listenable` object.
/// 
/// Works full with Riverpod and can use inside the `AnimationBuilder`.
final counterListenable = ChangeNotifierProvider((ref) => CounterNotifier());

class CounterNotifier with ChangeNotifier {
  int _counter = 0;
  int get counter => _counter;

  void increment() {
    _counter++;
    notifyListeners();
  }
}

/// Provide `ValueListenable`.
///
/// Rebuilds only inside the `ValueListenableBuilder` or `AnimatedBuilder`.
final counterValueListenable = Provider((ref) => Counter());

class Counter extends ValueNotifier<int>{
  Counter() : super(0);

  void increment() => value++;
}
rrousselGit commented 2 years ago

I'll close this, as Riverpod is actively trying to reduce its API surface. People complain about having too many providers. Adding more would be counter-productive, especially considering it is fairly niche and with some workarounds available.

timcreatedit commented 4 months ago

@rrousselGit I think there is a strong case to add something like ValueListenableProvider for two reasons.

  1. Forcing people to rely on riverpod-internal Notifiers to use them with providers promotes a walled-garden ecosystem. Anything that relies on ValueNotifier to manage state (such as VideoPlayerController) can not easily be exposed through riverpod. In our current team for example, we want to build a reusable MQTT solution, that automatically unsubscribes from MQTT topics using .autoDispose Providers. If we want to do that now, we either need to rely on ChangeNotifier (undesirable for obvious reasons), or implement the logic using AutoDisposeNotifier which forces us to use riverpod anywhere where we want to use the MQTT solution.
  2. Riverpod has ChangeNotifierProvider already, which makes sense for compatibility with code outside of the ecosystem, but is arguably quite a bad match for riverpod, since it contains mutable state. Thus, it feels quite inappropriate to support ChangeNotifier but not ValueNotifier