Skip to content

StreamSignal

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