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
errordescribing what went wrong.constfactory - ApiResponse.loading()
-
Creates a loading state.
constfactory
- ApiResponse.success(T data)
-
Creates a success state with
dataas the payload.constfactory
Properties
- dataOrNull → T?
-
Returns the response data, or
nullwhen not in the success state.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isFailure → bool
-
Returns
truewhen the request failed.no setter - isLoading → bool
-
Returns
truewhile the request is in-flight.no setter - isSuccess → bool
-
Returns
truewhen 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
transformto the success payload, producing an ApiResponse of the new typeR. -
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