AsyncState<T> class sealed

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 the state has a value - AsyncData.

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

.hasError

Returns true if the state has a error - AsyncError.

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

.isRefreshing

Returns true if the state is refreshing - AsyncDataRefreshing or AsyncErrorRefreshing.

final s = asyncSignal<int>(AsyncState.loading());
print(s.isRefreshing); // false
s.value = AsyncState.errorRefreshing('error');
print(s.isRefreshing); // true
s.value = AsyncState.dataRefreshing(1);
print(s.isRefreshing); // true

.isReloading

Returns true if the state is refreshing - AsyncDataReloading or AsyncErrorReloading.

final s = asyncSignal<int>(AsyncState.loading());
print(s.isReloading); // false
s.value = AsyncState.dataReloading(1);
print(s.isReloading); // true
s.value = AsyncState.errorReloading('error');
print(s.isReloading); // true

.requireValue

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

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

.value

Return the current value if exists - AsyncData.

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

.error

Return the current error if exists - AsyncError.

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

.stackTrace

Return the current stack trace if exists - AsyncError.

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

Constructors

AsyncState.data(T data)
Create a state with a value
factory
AsyncState.dataRefreshing(T data)
Create a state with a value that is refreshing
factory
AsyncState.dataReloading(T data)
Create a state with a value that is reloading
factory
AsyncState.error(Object error, [StackTrace? stackTrace])
Create a state with an error
factory
AsyncState.errorRefreshing(Object error, [StackTrace? stackTrace])
Create a state with an error that is refreshing
factory
AsyncState.errorReloading(Object error, [StackTrace? stackTrace])
Create a state with an error that is reloading
factory
AsyncState.loading()
Create a loading state
factory

Properties

error → Object?
Returns the error of the state.
no setter
hasError → bool
Returns true if the state has an error
no setter
hashCode → int
The hash code for this object.
no setteroverride
hasValue → bool
Returns true if the state has a value
no setter
isLoading → bool
Check if the state is a loading state
no setter
isRefreshing → bool
Returns true if the state is refreshing with a loading flag, has a value or error and is not the loading state
no setter
isReloading → bool
Returns true if the state is reloading with having a value or error, and is the loading state
no setter
requireValue → T
Force unwrap the value of the state.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
stackTrace → StackTrace?
Returns the stack trace of the state.
no setter
value → T?
Returns the value of the state.
no setter

Methods

map<E>({required AsyncDataBuilder<E, T> data, required AsyncErrorBuilder<E> error, required AsyncStateBuilder<E> loading, AsyncStateBuilder<E>? reloading, AsyncStateBuilder<E>? refreshing}) → E
Map the state to a value.
maybeMap<E>({AsyncDataBuilder<E, T>? data, AsyncErrorBuilder<E>? error, AsyncStateBuilder<E>? loading, AsyncStateBuilder<E>? reloading, AsyncStateBuilder<E>? refreshing, required AsyncStateBuilder<E> orElse}) → E
Map the state to a value with optional or else.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(covariant AsyncState<T> other) → bool
The equality operator.
override