ApiResponse<T> class sealed

A discriminated union modelling the three states of an asynchronous API call: loading, success, and failure.

Construct via the factory constructors:

ApiResponse<User> state = const ApiResponse.loading();
state = ApiResponse.success(user);
state = ApiResponse.failure(NetworkException(message: 'Timeout'));

Exhaustively handle states with when:

final widget = response.when(
  loading: () => const CircularProgressIndicator(),
  success: (user) => UserCard(user: user),
  failure: (error) => ErrorView(message: error.userMessage),
);
Implementers

Constructors

ApiResponse.failure(PrimekitException error)
Creates a failure state with error describing what went wrong.
const
factory
ApiResponse.loading()
Creates a loading state.
const
factory
ApiResponse.success(T data)
Creates a success state with data as the payload.
const
factory

Properties

dataOrNull → T?
Returns the response data, or null when not in the success state.
no setter
hashCode int
The hash code for this object.
no setterinherited
isFailure bool
Returns true when the request failed.
no setter
isLoading bool
Returns true while the request is in-flight.
no setter
isSuccess bool
Returns true when the request succeeded.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

map<R>(R transform(T data)) ApiResponse<R>
Applies transform to the success payload, producing an ApiResponse of the new type R.
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 loading(), required R success(T data), required R failure(PrimekitException error)}) → R
Exhaustively maps each state to a value of type R.

Operators

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