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
Available extensions

Constructors

StreamSignal(Stream<T> fn(), {AsyncSignalOptions<T>? options, @Deprecated('Use options: AsyncSignalOptions(cancelOnError: ...) instead') bool? cancelOnError, @Deprecated('Use options: AsyncSignalOptions(initialValue: ...) instead') T? initialValue, @Deprecated('Use options: AsyncSignalOptions(dependencies: ...) instead') List<ReadonlySignal>? dependencies, @Deprecated('Use options: AsyncSignalOptions(onDone: ...) instead') void onDone()?, @Deprecated('Use options: AsyncSignalOptions(lazy: ...) instead') bool? lazy, @Deprecated('Use options: AsyncSignalOptions(autoDispose: ...) instead') bool? autoDispose, @Deprecated('Use options: AsyncSignalOptions(name: ...) instead') String? debugLabel})
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.
getter/setter pairinherited
cancelOnError bool?
Cancel the subscription on error
latefinal
debugLabel String?
Debug label for Debug Mode Debug label for Debug Mode
no setterinherited
dependencies List<ReadonlySignal>
List of dependencies to recompute the stream
latefinal
disposed bool
Check if the effect is disposed
getter/setter pairinherited
equalityCheck → SignalEquality<AsyncState<T>>
Optional method to check if to values are the same
no setterinherited
first Future<T>
Last value of the stream
no setter
future Future<T>
The future of the signal completer
no setterinherited
globalId int
finalinherited
hashCode int
The hash code for this object.
no setterinherited
internalValue AsyncState<T>
@internal Internal getter for the raw value without subscription tracking.
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 a signal value is set (does not subscribe)
no setterinherited
isPaused bool
Check if the subscription is paused
no setter
last Future<T>
First value of the stream
no setter
name String?
finalinherited
requireValue → T
Returns the value of the signal
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
unwatched → void Function()?
finalinherited
value AsyncState<T>
Gets the current value of the signal.
getter/setter pairinherited-setteroverride-getter
version int
Version numbers should always be >= 0, because the special value -1 is used by Nodes to signify potentially unused but recyclable nodes.
getter/setter pairinherited
watched → void Function()?
finalinherited

Methods

add(T event) → void
Adds a data event to the sink.
inherited
addError(Object error, [StackTrace? stackTrace]) → void
Adds an error to the sink.
inherited
call() AsyncState<T>
Return the value when invoked
inherited
cancel() Future<void>
Cancel the subscription
close() → void
Closes the sink.
inherited
dispose() → void
Dispose the signal
override
execute(Stream<T> src) Future<void>
Execute the stream
get() AsyncState<T>
Helper method to get the current value
inherited
init() → void
Initialize the signal
inherited
internalRefresh() bool
@internal Refreshes the signal's value internally.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(void cleanup()) → void Function()
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
selectData<R>(R selector(T data)) Computed<AsyncState<R>>

Available on Signal<AsyncState<T>>, provided by the AsyncSignalState extension

Select from data when available, preserving async state
set(AsyncState<T> val, {bool force = false}) bool
Updates the signal's value by method call.
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)) → void Function()
Subscribe to value changes with a dispose function
inherited
subscribeToNode(Node node) → void
@internal Subscribes this signal to notifications from a given dependency node.
inherited
toJson() → dynamic
Convert value to JSON
inherited
toString() String
A string representation of this object.
inherited
unsubscribeFromNode(Node node) → void
@internal Unsubscribes this signal from notifications from a given dependency node.
inherited

Operators

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