RequestStatus<ResultType> class
When one is dealing with ui responses to different request states, in the course of it,
usually there are four states of interest Idle, Loading, Succeded
or Failed.
So the convenience generic union type
RequestStatus<ResultType>
serves the purpose of modeling those states. Idle and Loading, carry no inner state, but
Succeeded<ResultType>().data = ResultType data;
contains a field data
of type ResultType
. And the
Failed().error = AppError error;
contains a field error
of type AppError
. Where AppError
is the convenience type
that models errors in the app.
To deal with the request states one should use one of the unions methods.
The .map
forces you to deal with all the four states explicitly, passing callbacks for
each state with undestructured states.
Example:
Widget build(context) {
final someRequestStatus = someStateManagement.desiredRequestStatus;
return someRequestStatus.map(
idle: (idle) => "widget for idle state",
loading: (loading) => "widget for loading state",
succeeded: (succeeded) => "widget for succeeded state using possibly data within succeeded.data",
failed: (failed) => "widget for failed state using possibly AppError within failed.error",
);
}
The .when
forces you to deal with all the four states explicitly, passing callbacks for
each state with destructured states.
/// Example:
Widget build(context) {
final someRequestStatus = someStateManagement.desiredRequestStatus;
return someRequestStatus.when(
idle: () => "widget for idle state",
loading: () => "widget for loading state",
succeeded: (data) => "widget for succeeded state using possibly data within data",
failed: (error) => "widget for failed state using possibly AppError within error",
);
}
and one also might want to not deal explicitly with all states diferently, so
there are the .maybeMap
, and .maybeWhen
methods where you need only expclitly to pass a
orElse
callback.
Example:
Widget build(context) {
final someRequestStatus = someStateManagement.desiredRequestStatus;
return someRequestStatus.maybeWhen(
orElse: () => "default widget to be displayed other wise the current state is not specified in other callbacks"
loading: () => "widget for loading state",
succeeded: (data) => "widget for succeeded state using possibly data within succeeded.data",
);
}
So, RequestStatus
provides a safe and declarative way to always deal with all possible states of a request.
Constructors
- RequestStatus.failed(AppError error)
-
Type representing the Failed state of a request when it is not successful, bringing possibly the modeled error
constfactory
-
RequestStatus.fromResult(Result<
ResultType> result) -
Factory for helping building a RequestStatus from a Result input. It produces a Failed status if the input is a Failure, and a Succeeded otherwise
factory
- RequestStatus.idle()
-
Type representing the Idle state of a request still not fired
constfactory
- RequestStatus.loading()
-
Type representing the Loading state of a request when it is waiting for a response
constfactory
- RequestStatus.succeeded(ResultType data)
-
Type representing the Succeeded state of a request when it is successful, bringing possibly the modeled response data
constfactory
Properties
- asFailed → Failed
-
Cast
this
into a Failed, and throw an exception if the cast fails!It might be tempting to just cast the RequestStatus into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - asIdle → Idle
-
Cast
this
into an Idle, and throw an exception if the cast fails!It might be tempting to just cast the RequestStatus into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - asLoading → Loading
-
Cast
this
into a Loading, and throw an exception if the cast fails!It might be tempting to just cast the RequestStatus into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - asSucceeded → Succeeded
-
Cast
this
into a Succeeded, and throw an exception if the cast fails!It might be tempting to just cast the RequestStatus into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isFailed → bool
-
no setter
- isIdle → bool
-
no setter
- isLoading → bool
-
no setter
- isSucceeded → bool
-
no setter
-
maybeData
→ Maybe<
ResultType> -
Getter that results in a Maybe that is Just if the RequestStatus is Succeeded and Nothing otherwise
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
map<
TResult extends Object?> ({required TResult idle(Idle< ResultType> value), required TResult loading(Loading<ResultType> value), required TResult succeeded(Succeeded<ResultType> value), required TResult failed(Failed<ResultType> value)}) → TResult -
inherited
-
mapOrNull<
TResult extends Object?> ({TResult? idle(Idle< ResultType> value)?, TResult? loading(Loading<ResultType> value)?, TResult? succeeded(Succeeded<ResultType> value)?, TResult? failed(Failed<ResultType> value)?}) → TResult? -
inherited
-
maybeMap<
TResult extends Object?> ({TResult idle(Idle< ResultType> value)?, TResult loading(Loading<ResultType> value)?, TResult succeeded(Succeeded<ResultType> value)?, TResult failed(Failed<ResultType> value)?, required TResult orElse()}) → TResult -
inherited
-
maybeWhen<
TResult extends Object?> ({TResult idle()?, TResult loading()?, TResult succeeded(ResultType data)?, TResult failed(AppError error)?, required TResult orElse()}) → TResult -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
when<
TResult extends Object?> ({required TResult idle(), required TResult loading(), required TResult succeeded(ResultType data), required TResult failed(AppError error)}) → TResult -
inherited
-
whenOrNull<
TResult extends Object?> ({TResult? idle()?, TResult? loading()?, TResult? succeeded(ResultType data)?, TResult? failed(AppError error)?}) → TResult? -
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited