Result<S extends Object, F extends Object> class
sealed
A Result represents the result of an operation. It is a Success or Failure.
Results are an alternative error-handling mechanism to exceptions. It is inspired by other languages such as Rust and Swift.
To represent a success:
Result<String, int> httpGet() {
if (successful) return Success('HTTP body in plain text');
}
To represent a failure:
Result<String, int> httpGet() {
if (notFound) return Failure(404);
}
You can also perform pattern matching on a Result
.
void handle() {
switch (httpGet()) {
case Success(:final success):
// handle success
case Failure(:final failure):
// handle failure
}
}
See Maybe for representing a value and the possible absence thereof.
Properties
- failure → F?
-
The value if this is a
Failure
ornull
otherwise.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- success → S?
-
The value if this is a
Success
ornull
otherwise.no setter
Methods
-
bind<
T extends Object> (Result< T, F> function(S success)) → Result<T, F> -
If this is a
Success
, mapsS
to Result, otherwise returnsF
untouched. -
bindFailure<
T extends Object> (Result< S, T> function(F failure)) → Result<S, T> -
If this is a
Failure
, mapsF
to Result, otherwise returnsS
untouched. -
map<
T extends Object> (T function(S success)) → Result< T, F> -
If this is a
Success
, mapsS
toT
, otherwise returnsF
untouched. -
mapFailure<
T extends Object> (T function(F failure)) → Result< S, T> -
If this is a
Failure
, mapsF
toT
, otherwise returnsS
untouched. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pipe<
T extends Object> (Future< Result< function(S success)) → Future<T, F> >Result< T, F> > -
If this is a
Success
, asynchronously mapsS
to Result, otherwise returnsF
untouched. -
pipeFailure<
T extends Object> (Future< Result< function(F failure)) → Future<S, T> >Result< S, T> > -
If this is a
Failure
, asynchronously mapsF
to Result, otherwise returnsS
untouched. -
toString(
) → String -
A string representation of this object.
inherited
-
when(
{Consume< S> success = _nothing, Consume<F> failure = _nothing}) → void -
Calls
success
if this is aSuccess
, orfailure
if this is aFailure
.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited