Closed funwithflutter closed 2 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.
It isn't exported and is an implementation detail
Seems to be exported through hooks_riverpod. That's how I stumbled on it. I was looking for ValueListenableProvider, and found ValueProvider.
That's a mistake. I will hide it
Hello @rrousselGit any updates on the status of this issue( ListenableProvider)
No
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
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++;
}
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.
@rrousselGit I think there is a strong case to add something like ValueListenableProvider
for two reasons.
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.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
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?