toValueNotifier method
Converts the current stream into a ValueListenable<T>
which is effectively a ValueNotifier<T>
.
The conversion process involves listening to the stream and updating the ValueNotifier
's value
each time the stream emits a new item. This allows Flutter widgets to reactively rebuild whenever
the ValueNotifier
's value changes, based on the latest data emitted by the stream.
Parameters:
initialValue
: The initial value to be used for theValueNotifier
before any data is received from the stream.onDone
: An optional callback that gets called when the stream is done. The last value received from the stream is passed to this callback.onError
: An optional error handler for stream errors. If not provided, a default error handler that logs the error is used.
Returns a ValueListenable<T>
which is a ValueNotifier<T>
that updates its value based on the stream's emissions.
Implementation
ValueListenable<T> toValueNotifier(
T initialValue, {
void Function(T)? onDone,
void Function(Object, StackTrace)? onError,
}) {
final notifier = ValueNotifier<T>(initialValue);
listen(
(value) => notifier.value = value,
onError: onError ?? _defaultOnError,
onDone: () => onDone?.call(notifier.value),
);
return notifier;
}