ApiState<T, E> class sealed

Represents the state of an API request

Type Parameters: T - Success data type E - Custom error type (optional, defaults to ApiError)

States:

  • idle: Initial state, no request made yet
  • loading: Request in progress
  • success: Request completed successfully with data
  • empty: Request succeeded but data is empty (list/null)
  • failed: Request failed with error details
  • networkError: Request failed due to network issues

Example:

ApiState<List<Gallery>, ApiError> state = ApiState.idle();

// Pattern matching
state.when(
  idle:         ()        => const SizedBox.shrink(),
  loading:      ()        => const GalleryShimmer(),
  success:      (items)   => GalleryGrid(items),
  empty:        ()        => const EmptyGalleries(),
  failed:       (error)   => ErrorView(error.message),
  networkError: (error)   => const NoInternetView(),
);

// Or use the UI helper
state.buildWidget(
  loading:      ()        => const GalleryShimmer(),
  success:      (items)   => GalleryGrid(items),
  empty:        ()        => const EmptyGalleries(),
  error:        (msg)     => ErrorView(msg, onRetry: _reload),
  networkError: ()        => NoInternetView(onRetry: _reload),
);
Implementers
Available extensions

Constructors

ApiState.empty()
Empty state – request succeeded but data is empty (auto-emitted by ApiExecutor when emptyCheck returns true)
const
factory
ApiState.failed(FailureResponse<E> error)
Failed state – request failed with structured 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 connectivity issues
const
factory
ApiState.success(T data)
Success state – request completed with data
const
factory

Properties

data → T?
Success data, or null if not in success state
no setter
dataOrNull → T?

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

no setter
error FailureResponse<E>?
Error payload for failed / networkError states, null otherwise
no setter
errorOrNull FailureResponse<E>?

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

no setter
hasData bool

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

no setter
hasError bool

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

no setter
hashCode int
The hash code for this object.
no setterinherited
isEmpty bool

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

no setter
isError bool

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

True for any error state (failed OR networkError)
no setter
isFailed bool

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

no setter
isIdle bool

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

no setter
isLoading bool

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

no setter
isNetworkError bool

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

no setter
isSuccess bool

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

no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

buildWidget({required Widget loading(), required Widget success(T data), required Widget error(String message), Widget empty()?, Widget networkError()?, Widget idle()?}) Widget

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

Build a Flutter widget directly from state – eliminates screen boilerplate.
map<R>(R transform(T data)) ApiState<R, E>
Map success data to a different type, preserving all other states
maybeWhen<R>({R idle()?, R loading()?, R success(T data)?, R empty()?, R failed(FailureResponse<E> error)?, R networkError(FailureResponse<E> error)?, required R orElse()}) → R
Partial pattern match – unhandled states fall through to orElse
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 empty(), required R failed(FailureResponse<E> error), required R networkError(FailureResponse<E> error)}) → R
Exhaustive pattern match over all states

Operators

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