stream_listenable
Bridge Dart Streams into Flutter's Listenable / ValueListenable interfaces.
Wraps any Stream<T> so it can be used with ListenableBuilder, ValueListenableBuilder, AnimatedBuilder, and any other widget or API that accepts a Listenable.
Features
StreamListenable<T>— implementsListenable. Notifies listeners on every stream emission.StreamValueListenable<T>— implementsValueListenable<T>. Tracks the latest value and notifies on changes.- Extension methods —
stream.asListenable()andstream.asValueListenable()for concise usage. - Lazy subscription — subscribes when the first listener is added, cancels when the last is removed.
Getting started
dependencies:
stream_listenable: ^1.0.0
Usage
As a Listenable
final listenable = myStream.asListenable();
ListenableBuilder(
listenable: listenable,
builder: (context, child) {
// Rebuilds on every stream emission
return Text('Stream emitted');
},
);
As a ValueListenable
final valueListenable = myStream.asValueListenable(initialValue: 0);
ValueListenableBuilder<int>(
valueListenable: valueListenable,
builder: (context, value, child) {
return Text('Value: $value');
},
);
Options
// Notify listeners on stream errors and completion
StreamListenable(myStream, notifyOnError: true, notifyOnDone: true);
// Notify even when the same value is emitted consecutively
StreamValueListenable(myStream, initialValue: 0, notifyOnSameValue: true);
Additional information
- Looking for BLoC support? See bloc_listenable.
- File issues at github.com/Jei-sKappa/stream_listenable/issues.