StreamSignal<T> class

Stream signals can be created by extension or method.

streamSignal

final stream = () async* {
    yield 1;
};
final s = streamSignal(() => stream);

toSignal()

final stream = () async* {
    yield 1;
};
final s = stream.toSignal();

.value, .peek()

Returns AsyncState<T> for the value and can handle the various states.

The value getter returns the value of the stream if it completed successfully.

.peek() can also be used to not subscribe in an effect

final stream = (int value) async* {
    yield value;
};
final s = streamSignal(() => stream);
final value = s.value.value; // 1 or null

.reset()

The reset method resets the stream to its initial state to recall on the next evaluation.

final stream = (int value) async* {
    yield value;
};
final s = streamSignal(() => stream);
s.reset();

.refresh()

Refresh the stream value by setting isLoading to true, but maintain the current state (AsyncData, AsyncLoading, AsyncError).

final stream = (int value) async* {
    yield value;
};
final s = streamSignal(() => stream);
s.refresh();
print(s.value.isLoading); // true

.reload()

Reload the stream value by setting the state to AsyncLoading and pass in the value or error as data.

final stream = (int value) async* {
    yield value;
};
final s = streamSignal(() => stream);
s.reload();
print(s.value is AsyncLoading); // true

Dependencies

By default the callback will be called once and the stream will be cached unless a signal is read in the callback.

final count = signal(0);
final s = streamSignal(() async* {
    final value = count();
    yield value;
});

await s.future; // 0
count.value = 1;
await s.future; // 1

If there are signals that need to be tracked across an async gap then use the dependencies when creating the streamSignal to reset every time any signal in the dependency array changes.

final count = signal(0);
final s = streamSignal(
    () async* {
        final value = count();
        yield value;
    },
    dependencies: [count],
);
s.value; // state with count 0
count.value = 1; // resets the future
s.value; // state with count 1

@link https://dartsignals.dev/async/stream

Inheritance

Constructors

StreamSignal(Stream<T> callback(), {bool? cancelOnError, String? debugLabel, T? initialValue, List<ReadonlySignal> dependencies = const [], void onDone()?, bool lazy = true, bool autoDispose = false})
Stream signals can be created by extension or method.

Properties

autoDispose bool
Throws and error if read after dispose and can be disposed on last unsubscribe.
finalinherited
cancelOnError bool?
Cancel the subscription on error
final
debugLabel String?
Debug label for Debug Mode
finalinherited
dependencies List<ReadonlySignal>
List of dependencies to recompute the stream
final
disposed bool
Returns true if dispose has been called and will throw and error on value read
getter/setter pairinherited
equalityCheck bool Function(AsyncState<T> a, AsyncState<T> b)
Optional method to check if to values are the same
getter/setter pairinherited
first Future<T>
Last value of the stream
no setter
future Future<T>
The future of the signal completer
no setterinherited
globalId int
Global ID of the signal
finalinherited
hashCode int
The hash code for this object.
no setterinherited
hasTargets bool
Check if there are any targets attached
no setterinherited
initialValue AsyncState<T>
Value that the signal was created with
no setterinherited
isCompleted bool
Returns true if the signal is completed an error or data
no setterinherited
isDone bool
Check if the signal is done
no setter
isInitialized bool
Check if the signal is lazy and has not had a value set
no setterinherited
isPaused bool
Check if the subscription is paused
no setter
last Future<T>
First value of the stream
no setter
previousValue AsyncState<T>?
Previous value that was set before the current
no setterinherited
requireValue → T
Returns the value of the signal
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
targets Iterable<SignalListenable>
@internal for testing getter to track all the effects currently effected in the signal
no setterinherited
value AsyncState<T>
Compute the current value
getter/setter pairinherited-setteroverride-getter
version int
Version number is used to track changes and will increment for every set
no setterinherited

Methods

call() AsyncState<T>
Return the value when invoked
inherited
cancel() Future<void>
Cancel the subscription
dispose() → void
Dispose the signal
override
execute(Stream<T> src) Future<void>
Execute the stream
forceUpdate([AsyncState<T>? val]) → void
Force update a value
inherited
get() AsyncState<T>
Get the current value
inherited
init() → void
Initialize the signal
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(void cleanup()) EffectCleanup
Add a cleanup function to be called when the signal is disposed
inherited
overrideWith(AsyncState<T> val) Signal<AsyncState<T>>
Override the current signal with a new value as if it was created with it
inherited
pause([Future<void>? resume]) → void
Pause the subscription
peek() AsyncState<T>
In the rare instance that you have an effect that should write to another signal based on the previous value, but you don't want the effect to be subscribed to that signal, you can read a signals's previous value via signal.peek().
inherited
readonly() ReadonlySignal<AsyncState<T>>
Returns a readonly signal
inherited
refresh() Future<void>
Refresh the future
override
reload() Future<void>
Reload the future
override
reset([AsyncState<T>? value]) → void
Reset the signal to the initial value
override
resume() → void
Resume the subscription
set(AsyncState<T> val, {bool force = false}) bool
Update the current value.
inherited
setError(Object error, [StackTrace? stackTrace]) → void
Set the error with optional stackTrace to AsyncError
override
setLoading([AsyncState<T>? state]) → void
Set the loading state to AsyncLoading
inherited
setValue(T value) → void
Set the value to AsyncData
inherited
subscribe(void fn(AsyncState<T> value)) EffectCleanup
Subscribe to value changes
inherited
toJson() → dynamic
Convert value to JSON
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited