Result<T, E> class
sealed
Result is a type that that represents either success (Ok) or failure (Err)
Examples
Basic usage:
class FallibleOpSuccess {}
class FallibleOpFailure {}
Result<FallibleOpSuccess, FallibleOpFailure> fallibleOp() {
if (true) {
return Ok(FallibleOpSuccess());
} else {
return Err(FallibleOpFailure());
}
}
final result = fallibleOp();
result.inspect((value) {
print('Success with value: $value');
}).inspectErr((error) {
print('Failure with error: $error');
});
}
Properties
Methods
-
and<
U> (Result< U, E> res) → Result<U, E> -
Return
resif the result isOk, otherwise returns theErrvalue ofthis. -
andThen<
U> (Result< U, E> okMap(T value)) → Result<U, E> -
Calls
opif the result isOk, otherwise returns theErrvalue ofthis. -
contains(
Object? x) → bool -
Returns
trueif the result is anOk(success) value containing the given value. -
containsErr(
Object? x) → bool -
Returns
trueif the result is anErr(failure) value containing the given value. -
err(
) → E? -
Converts from
Result<T, E>toE?. -
expect(
String message) → T -
Returns the contained
Ok(success) value. -
expectErr(
String message) → E -
Returns the contained
Err(failure) value. -
inspect(
void f(T value)) → Result< T, E> -
Calls the provided closure with the contained value (if
Ok) -
inspectErr(
void f(E error)) → Result< T, E> -
Calls the provided closure with the contained error (if
Err). -
isErrAnd(
bool f(E error)) → bool -
Returns
trueif the result isErrand the value matches the predicatef -
isOkAnd(
bool f(T value)) → bool -
Returns
trueif the result isOkand the value matches the predicatef -
map<
U> (U okMap(T value)) → Result< U, E> -
Maps a
Result<T, E>toResult<U, E>by applying a function to a containedOk(success) value, and leaving anErr(failure) value untouched. -
mapErr<
F> (F errMap(E error)) → Result< T, F> -
Maps a
Result<T, E>toResult<T, F>by applying a function to a containedErr(failure) value, leaving anOk(success) value untouched. -
mapOr<
U> ({required U fallback, required U okMap(T value)}) → U -
Returns the provided fallback (if
Err(failure)), or applies a function to the contained value (ifOk(success)). -
mapOrElse<
U> ({required U errMap(E error), required U okMap(T value)}) → U -
Maps a
Result<T, E>toUby applying fallback functionfallbackto a containedErr(failure) value, or functionopto a containedOk(success) value. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
ok(
) → T? -
Converts from
Result<T, E>toT?. -
or<
F> (Result< T, F> res) → Result<T, F> -
Returns
resif the result isErr, otherwise returns theOk(success) value ofthis. -
orElse<
F> (Result< T, F> errMap(E error)) → Result<T, F> -
Calls
opif the result isErr, otherwise returns theOkvalue ofthis. -
toString(
) → String -
A string representation of this object.
inherited
-
unwrap(
) → T -
Returns the contained
Ok(success) value. Because this function may throw an exception, its use is generally discouraged. Instead, prefer to useswitchstatements with theResult.typeand handle theErr(failure) case explicitly, or call`unwrapOr`,`unwrapOrElse`. -
unwrapErr(
) → E -
Returns the contained
Err(failure) value. -
unwrapOr(
T fallback) → T -
Returns the contained
Ok(success) value or a provided fallback. -
unwrapOrElse(
T fallback(E error)) → T -
Returns the contained
Ok(success) value or computes it from the closure. -
when<
U> ({required U ok(T value), required U err(E error)}) → U -
Maps a
Result<T, E>toU
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited