ApiState<T, E> class
sealed
Represents the state of an API request
Type Parameters: T - Success data type E - Custom error type (optional)
States:
- idle: Initial state, no request made yet
- loading: Request in progress
- success: Request completed successfully with data
- failed: Request failed with error details
- networkError: Request failed due to network issues
Example:
ApiState<User, LoginError> state = ApiState.idle();
state = ApiState.loading();
state = ApiState.success(user);
// or
state = ApiState.failed(FailureResponse(...));
// or
state = ApiState.networkError(FailureResponse(...));
// Pattern matching
state.when(
idle: () => Text('Ready'),
loading: () => CircularProgressIndicator(),
success: (user) => UserProfile(user),
failed: (error) => ErrorWidget(error.message),
networkError: (error) => NoInternetWidget(),
);
- Implementers
- Available extensions
Constructors
-
ApiState.failed(FailureResponse<
E> error) -
Failed state - request failed with error
constfactory
- ApiState.idle()
-
Initial state - no request made yet
constfactory
- ApiState.loading()
-
Loading state - request in progress
constfactory
-
ApiState.networkError(FailureResponse<
E> error) -
Network error state - request failed due to network issues
constfactory
- ApiState.success(T data)
-
Success state - request completed with data
constfactory
Properties
- data → T?
-
Get data if in success state, null otherwise
no setter
- dataOrNull → T?
-
Available on ApiState<
Get data or nullT, E> , provided by the ApiStateConvenience extensionno setter -
error
→ FailureResponse<
E> ? -
Get error if in failed/networkError state, null otherwise
no setter
-
errorOrNull
→ FailureResponse<
E> ? -
Available on ApiState<
Get error or nullT, E> , provided by the ApiStateConvenience extensionno setter - hasData → bool
-
Available on ApiState<
Check if has dataT, E> , provided by the ApiStateConvenience extensionno setter - hasError → bool
-
Available on ApiState<
Check if has errorT, E> , provided by the ApiStateConvenience extensionno setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isError → bool
-
Available on ApiState<
Check if is any error state (failed or network error)T, E> , provided by the ApiStateConvenience extensionno setter - isFailed → bool
-
Available on ApiState<
Check if is failedT, E> , provided by the ApiStateConvenience extensionno setter - isIdle → bool
-
Available on ApiState<
Check if is idleT, E> , provided by the ApiStateConvenience extensionno setter - isLoading → bool
-
Available on ApiState<
Check if is loadingT, E> , provided by the ApiStateConvenience extensionno setter - isNetworkError → bool
-
Available on ApiState<
Check if is network errorT, E> , provided by the ApiStateConvenience extensionno setter - isSuccess → bool
-
Available on ApiState<
Check if is successT, E> , provided by the ApiStateConvenience extensionno setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
map<
R> (R transform(T data)) → ApiState< R, E> - Map the success data to a new type
-
maybeWhen<
R> ({R idle()?, R loading()?, R success(T data)?, R failed(FailureResponse< E> error)?, R networkError(FailureResponse<E> error)?, required R orElse()}) → R - Pattern matching with optional handlers
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
when<
R> ({required R idle(), required R loading(), required R success(T data), required R failed(FailureResponse< E> error), required R networkError(FailureResponse<E> error)}) → R - Pattern matching for all states
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited