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
- Inheritance
-
- Object
- Signal<
AsyncState< T> > - AsyncSignal<
T> - StreamSignal
Constructors
-
StreamSignal(Stream<
T> fn(), {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.
getter/setter pairinherited
- brand → Symbol
-
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
-
Check if the effect is disposed
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
-
internalValue
↔ AsyncState<
T> -
getter/setter pairinherited
- 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 value is set and not a lazy signal
getter/setter pairinherited
- isPaused → bool
-
Check if the subscription is paused
no setter
-
last
→ Future<
T> -
First value of the stream
no setter
- node ↔ Node?
-
getter/setter pairinherited
- requireValue → T
-
Returns the value of the signal
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- targets ↔ Node?
-
getter/setter pairinherited
-
value
↔ AsyncState<
T> -
Compute the current value
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
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 -
afterCreate(
AsyncState< T> val) → void -
Internal hook for after a signal is created
inherited
-
beforeUpdate(
AsyncState< T> val) → void -
Internal hook for after a signal is updated
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 -
inherited
-
internalSetValue(
AsyncState< T> val) → void -
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 -
Set the current value by a method
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 -
inherited
-
toJson(
) → dynamic -
Convert value to JSON
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unsubscribeFromNode(
Node node) → void -
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited