AsyncState<S extends Object?> class

Represents the state of an asynchronous operation, encapsulating loading, data, and error states.

AsyncState is useful for managing UI state in response to async tasks such as network requests, database operations, or any operation that can be in a loading, success, or error state.

S is the type of data managed by the state.

This class provides factory constructors for loading, data, and error states, and utility methods to transition between them. It also implements pattern matching via when for ergonomic UI code. The class supports equality comparison and provides readable string representations for debugging.

Inheritance

Constructors

AsyncState.data(S? data)
Creates a data state with the given data.
const
AsyncState.error(Object error, {String? message, StackTrace? stackTrace})
Creates an error state with the given error information.
AsyncState.errorWithState(ErrorState? errorState)
Creates an error state with an existing ErrorState.
const
AsyncState.loading()
Creates a loading state.
const

Properties

data → S?
The data held by the state, if any.
finalinherited
errorState ErrorState?
The error information held by the state, if any.
finalinherited
hasError bool
Returns true if the state represents an error.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isLoading bool
Returns true if the state is loading.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toData(S data) AsyncState<S>
Returns a new AsyncState with the given data.
override
toError(Object error, {String? message, StackTrace? stackTrace}) AsyncState<S>
Returns a new AsyncState with the given error information.
override
toLoading() AsyncState<S>
Returns a new AsyncState representing the loading state.
override
toString() String
A string representation of this object.
override
when<T>({required T loading(), required T data(S data), required T error(ErrorState e)}) → T
Pattern matching for async state.
override
whenData<T>(void data(S data), {void loading()?, void error(ErrorState e)?}) → void
Optional pattern matching for async state, focused on data extraction.
override

Operators

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