AsyncSignal<T> class

AsyncState is class commonly used with Future/Stream signals to represent the states the signal can be in.

AsyncSignal

AsyncState is the default state if you want to create a AsyncSignal directly:

final s = asyncSignal(AsyncState.data(1));
s.value = AsyncState.loading(); // or AsyncLoading();
s.value = AsyncState.error('Error', null); // or AsyncError();

AsyncState

AsyncState is a sealed union made up of AsyncLoading, AsyncData and AsyncError.

.future

Sometimes you need to await a signal value in a async function until a value is completed and in this case use the .future getter.

final s = asyncSignal<int>(AsyncState.loading());
s.value = AsyncState.data(1);
await s.future; // Waits until data or error is set

.isCompleted

Returns true if the future has completed with an error or value:

final s = asyncSignal<int>(AsyncState.loading());
s.value = AsyncState.data(1);
print(s.isCompleted); // true

.hasValue

Returns true if a value has been set regardless of the state.

final s = asyncSignal<int>(AsyncState.loading());
print(s.hasValue); // false
s.value = AsyncState.data(1);
print(s.hasValue); // true

.hasError

Returns true if a error has been set regardless of the state.

final s = asyncSignal<int>(AsyncState.loading());
print(s.hasError); // false
s.value = AsyncState.error('error', null);
print(s.hasError); // true

.isRefreshing

Returns true if the state is refreshing with a loading flag, has a value or error and is not the loading state.

final s = asyncSignal<int>(AsyncState.loading());
print(s.isRefreshing); // false
s.value = AsyncState.error('error', null, isLoading: true);
print(s.isRefreshing); // true
s.value = AsyncData(1, isLoading: true);
print(s.isRefreshing); // true

.isReloading

Returns true if the state is reloading with having a value or error, and is the loading state.

final s = asyncSignal<int>(AsyncState.loading());
print(s.isReloading); // false
s.value = AsyncState.loading(data: 1);
print(s.isReloading); // true
s.value = AsyncState.loading(error: ('error', null));
print(s.isReloading); // true

.requireValue

Force unwrap the value of the state and throw an error if it has an error or is null.

final s = asyncSignal<int>(AsyncState.data(1));
print(s.requireValue); // 1

.value

Return the current value if exists.

final s = asyncSignal<int>(AsyncState.data(1));
print(s.value); // 1 or null

.error

Return the current error if exists.

final s = asyncSignal<int>(AsyncState.error('error', null));
print(s.error); // 'error' or null

.stackTrace

Return the current stack trace if exists.

final s = asyncSignal<int>(AsyncState.error('error', StackTrace(...)));
print(s.stackTrace); // StackTrace(...) or null

.map

If you want to handle the states of the signal map will enforce all branching.

final signal = asyncSignal<int>(AsyncState.data(1));
signal.value.map(
 data: (value) => 'Value: $value',
 error: (error, stackTrace) => 'Error: $error',
 loading: () => 'Loading...',
);

.maybeMap

If you want to handle some of the states of the signal maybeMap will provide a default and optional overrides.

final signal = asyncSignal<int>(AsyncState.data(1));
signal.value.maybeMap(
 data: (value) => 'Value: $value',
 orElse: () => 'Loading...',
);

Pattern Matching

Instead of map and maybeMap it is also possible to use dart switch expressions to handle the branching.

final signal = asyncSignal<int>(AsyncState.data(1));
final value = switch (signal.value) {
    AsyncData<int> data => 'value: ${data.value}',
    AsyncError<int> error => 'error: ${error.error}',
    AsyncLoading<int>() => 'loading',
};

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

Inheritance
Mixed-in types

Constructors

AsyncSignal.new(AsyncState<T> value, {String? debugLabel, bool autoDispose = false})
A Signal that stores value in AsyncState

Properties

autoDispose bool
Throws and error if read after dispose and can be disposed on last unsubscribe.
getter/setter pairinherited
debugLabel String?
Debug label for Debug Mode
finalinherited
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
future Future<T>
The future of the signal completer
no setter
globalId int
Global ID of the signal
finalinherited
hashCode int
The hash code for this object.
no setterinherited
internalValue AsyncState<T>
no setterinherited
isCompleted bool
Returns true if the signal is completed an error or data
no setter
isInitialized bool
Check if the value is set and not a lazy signal
no setterinherited
requireValue → T
Returns the value of the signal
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
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
close() → void
Closes the sink.
inherited
dispose() → void
Dispose the signal
inherited
get() AsyncState<T>
Helper method to get the current value
inherited
init() → void
Initialize the signal
internalRefresh() bool
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
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
reload() Future<void>
Reload the future
reset([AsyncState<T>? value]) → void
Reset the signal to the initial value
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
setLoading([AsyncState<T>? state]) → void
Set the loading state to AsyncLoading
setValue(T value) → void
Set the value to AsyncData
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