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
const
factory
ApiState.idle()
Initial state - no request made yet
const
factory
ApiState.loading()
Loading state - request in progress
const
factory
ApiState.networkError(FailureResponse<E> error)
Network error state - request failed due to network issues
const
factory
ApiState.success(T data)
Success state - request completed with data
const
factory

Properties

data → T?
Get data if in success state, null otherwise
no setter
dataOrNull → T?

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Get data or null
no setter
error FailureResponse<E>?
Get error if in failed/networkError state, null otherwise
no setter
errorOrNull FailureResponse<E>?

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Get error or null
no setter
hasData bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if has data
no setter
hasError bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if has error
no setter
hashCode int
The hash code for this object.
no setterinherited
isError bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is any error state (failed or network error)
no setter
isFailed bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is failed
no setter
isIdle bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is idle
no setter
isLoading bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is loading
no setter
isNetworkError bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is network error
no setter
isSuccess bool

Available on ApiState<T, E>, provided by the ApiStateConvenience extension

Check if is success
no 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