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
res
if the result isOk
, otherwise returns theErr
value ofthis
. -
andThen<
U> (Result< U, E> okMap(T value)) → Result<U, E> -
Calls
op
if the result isOk
, otherwise returns theErr
value ofthis
. -
contains(
Object? x) → bool -
Returns
true
if the result is anOk
(success) value containing the given value. -
containsErr(
Object? x) → bool -
Returns
true
if 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
true
if the result isErr
and the value matches the predicatef
-
isOkAnd(
bool f(T value)) → bool -
Returns
true
if the result isOk
and 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>
toU
by applying fallback functionfallback
to a containedErr
(failure) value, or functionop
to 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
res
if the result isErr
, otherwise returns theOk
(success) value ofthis
. -
orElse<
F> (Result< T, F> errMap(E error)) → Result<T, F> -
Calls
op
if the result isErr
, otherwise returns theOk
value 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 useswitch
statements with theResult.type
and 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