stream_with_value 0.1.1 stream_with_value: ^0.1.1 copied to clipboard
An encapsulation of a Stream and a single value.
stream_with_value #
About #
The package provides the implementation of StreamWithValue that wraps a Stream and keeps the latest value that was received from it.
StreamWithLatestValue - implementation that wraps a Stream and keeps the latest value that was received from it. The value will not be tracked if there are no listeners on updates.
PushStreamWithValue - StreamWithValue implementation that creates a Stream from subsequent calls to add. This way, value is always set to the latest value that has been added, regardless of whether the updates are listened to (in contrast to StreamWithLatestValue).
How to use #
- Add
stream_with_value
to yourpubspec.yaml
:
dependencies:
stream_with_value: ^0.1.1
- Create StreamWithLatestValue
StreamController<int> _yourStreamController = StreamController<int>();
StreamWithLatestValue<int> _streamWithValue =
StreamWithLatestValue<int>.withInitialValue(_yourStreamController.stream, initialValue: 0);
- You can add new value to the stream using StreamController.
_yourStreamController.add(5);
- To get updates on the UI you can use
StreamBuilderWithValue
orDataStreamWithValueBuilder
widgets:
StreamBuilderWithValue<int>(
streamWithValue: _streamWithValue,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return (snapshot.hasData)
? Text(
'${snapshot.data ?? 0}',
style: Theme.of(context).textTheme.headline4,
)
: CircularProgressIndicator();
},
)
For more examples, please have a look at the example project.