RequestStatus<ResultType> class
sealed
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.
- Annotations
-
- @freezed
Constructors
- RequestStatus.failed(AppError error)
-
Type representing the
Failed
state of a request when it is not successful, bringing possibly the modeled errorconstfactory -
RequestStatus.fromResult(Result<
ResultType> result) -
Factory for helping building a RequestStatus from a Result input. It produces a
Failed
status if the input is aFailure
, and aSucceeded
otherwisefactory - RequestStatus.idle()
-
Type representing the
Idle
state of a request still not firedconstfactory - RequestStatus.loading()
-
Type representing the
Loading
state of a request when it is waiting for a responseconstfactory - RequestStatus.succeeded(ResultType data)
-
Type representing the
Succeeded
state of a request when it is successful, bringing possibly the modeled response dataconstfactory
Properties
- asFailed → Failed
-
Cast
this
into aFailed
, 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 anIdle
, 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 aLoading
, 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 aSucceeded
, 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 isSucceeded
andNothing
otherwiseno setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited