Result<ResultType> class
Every asynchronus task can have two possible outcomes as a Result. It is either a Success or a Failure. So the
Result<ResultType>
generic union type is a convenience type to model and help safelly deal with any asynchronus task outcomes
The approach is declarative, so in order to deal with the result, one should call the handle method which has two required parameters an onSuccess callback
Type onSuccess(Type data)
and an onFailure callback
Type onFailure(AppError data)
Where AppError is a convenience type to model errors in the application
Example:
Result<String> asyncTaskResturningStringResult = await someFutureOfResultString();
asyncTaskResturningStringResult.handle(
onSuccess: (String data) {
"here one have access to the succesful value of the async task and might use it as desired"
},
onFailure: (AppError error) {
"here one have access to the failure modeled as AppError representing this async task"
}
);
In this way one always needs to deal in a declarative way with both the success and failure possible outcomes as unfortunatelly any asynchronus task needs
Constructors
- Result.failure(AppError error)
-
Type representing the Failure case of a Result of a asynchronus task
possible outcome
constfactory
- Result.success(ResultType data)
-
Type representing the Success case of a Result of a asynchronus task
possible outcome
constfactory
Properties
-
asFailure
→ Failure<
AppError> -
Cast
thisinto a Failure, and throw an exception if the cast fails!It might be tempting to just cast the result into the desired type, but it'sstrongly advised to not do that. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter -
asSuccess
→ Success<
ResultType> -
Cast
thisinto a Success, and throw an exception if the cast fails!It might be tempting to just cast the result into the desired type, but it'sstrongly advised to not do that. 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
- isFailure → bool
-
no setter
- isSuccess → bool
-
no setter
-
maybeData
→ Maybe<
ResultType> -
Getter that results in a Just if the Result is Success and Nothing othterwise
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
handle<
T> ({required T onSuccess(ResultType data), required T onFailure(AppError error)}) → T - the handle method which has two required parameters an onSuccess callback
-
mapAsyncSuccess<
K> (FutureOr< Result< combiner(ResultType)) → FutureOr<K> >Result< K> > -
A method to chain asynchronous access to data held by the Result. If
thisis Failure returns[FutureOr<Failure>], ifthisis Success, returns the result of thecombinermethod over thedatainside Success -
mapSuccess<
K> (Result< K> combiner(ResultType)) → Result<K> -
A method used to chain access to data held by the Result. If
thisis Failure returns Failure, ifthisis Success, returns the result of thecombinermethod over thedatainside Success -
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