rxdart_flutter 0.0.1-dev.1
rxdart_flutter: ^0.0.1-dev.1 copied to clipboard
RxDart Flutter - Flutter Widgets that make it easy to use Streams with Flutter.
RxDart Flutter #
ValueStreamBuilder #
ValueStreamBuilder
is a widget similar to StreamBuilder
, but works with ValueStream
and simplifies the process of rebuilding widgets in response to stream updates. It provides a more streamlined API and performance improvements for working with streams that always have a value and do not emit errors.
Features #
- Works with
ValueStream
instead ofStream
. - Automatically rebuilds the widget when the stream emits new data.
- Supports optional
buildWhen
callback for more granular control over rebuild behavior.
Usage #
Basic Example
final valueStream = BehaviorSubject<int>.seeded(0);
ValueStreamBuilder<int>(
stream: valueStream,
builder: (context, data) {
// return widget here based on data
return Text('Current value: $data');
},
);
Example with buildWhen
You can provide an optional buildWhen
callback to control when the widget should be rebuilt based on changes to the data.
ValueStreamBuilder<int>(
stream: valueStream,
buildWhen: (previous, current) {
// Only rebuild if the current value is different from the previous value
return previous != current;
},
builder: (context, data) {
return Text('Current value: $data');
},
);
Parameters #
stream
: TheValueStream
to listen to. The stream must have a value at all times and must not emit errors.builder
: A callback that returns the widget to display based on the stream data.buildWhen
: An optional callback that determines whether to rebuild the widget based on the previous and current data. Defaults totrue
(always rebuilds).
Error Handling #
ValueStreamBuilder
requires the stream to always have a value and never emit errors. If an error occurs in the stream, it will be displayed using the ErrorWidget
.
If the stream has no value when the builder is first called, a ValueStreamHasNoValueError
will be thrown. You can handle this by ensuring that the stream is seeded with an initial value or by checking if the stream has a value before using ValueStreamBuilder
.