Result<T, E> class
sealed
A type that represents the result of something, either success (Ok) or failure (Err).
A Result type holds either a value of type T, or an error value of type E.
Pattern matching is recommended for interacting with Result types.
Result<int, String> foo = Ok(42);
print(switch (foo) {
Ok(value: var bar) => 'Ok value: $bar',
Err(value: var err) => 'Error value: $err'
});
See also:
Rust: Result
- Implementers
- Available extensions
Constructors
- Result.from(T? value, E error)
-
Creates a
Resultfrom the given nullableTvalue.factory
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
and<
U> (Result< U, E> other) → Result<U, E> -
Returns a
Resultvalue as Err<U, E> if thisResultis Err<T, E>, otherwise returnsother. -
andThen<
U> (Result< U, E> fn(T)) → Result<U, E> -
Returns a
Resultvalue as Err<U, E> if thisResultis Err<T, E>, otherwise callsfnwith the held Ok value and returns the returnedResult. -
call(
) → T - Shortcut to call Result.unwrap().
-
err(
) → Option< E> -
Converts this
Result<T, E>into an Option<E>, discarding the held value if this is Ok. -
expect(
String message) → T -
Returns the held value of this
Resultif it is Ok. Throws a ResultError with the givenmessageand held Err value if thisResultis Err. -
expectErr(
String message) → E -
Returns the held value of this
Resultif it is Err. Throws a ResultError with the givenmessageand held Ok value if thisResultis Ok. -
flatten(
) → Result< T, E> -
Available on Result<
Flattens a nestedResult< , provided by the ResultFlatten extensionT, E> , E>Resulttype value one level. -
inspect(
void fn(T)) → Result< T, E> -
Calls the provided function with the contained value if this
Resultis Ok. -
inspectErr(
void fn(E)) → Result< T, E> -
Calls the provided function with the contained error value if this
Resultis Err. -
isErr(
) → bool -
Returns whether or not this
Resultis Err. -
isErrAnd(
bool predicate(E)) → bool -
Returns whether or not this
Resultis Err and that the held error value matches the given predicate. -
isOk(
) → bool -
Returns whether or not this
Resultis Ok. -
isOkAnd(
bool predicate(T)) → bool -
Returns whether or not this
Resultis Ok and that the held value matches the given predicate. -
iter(
) → Iterable< T> - Returns an Iterable of the held value.
-
map<
U> (U mapFn(T)) → Result< U, E> -
Maps a
Result<T, E>to aResult<U, E>using the given function with the held value. -
mapErr<
F> (F mapFn(E)) → Result< T, F> -
Maps a
Result<T, E>to aResult<T, F>using the given function with the held value. -
mapOr<
U> (U orValue, U mapFn(T)) → Result< U, E> -
Maps a
Result<T, E>to aResult<U, E>using the given function with the held value if theResult<T, E>is Ok. Otherwise returns the providedorValueasOk(orValue). -
mapOrElse<
U> (U orFn(), U mapFn(T)) → Result< U, E> -
Maps a
Result<T, E>to aResult<U, E>using the givenmapFnfunction with the held value if theResultis Ok. Otherwise returns the result oforFnasOk(orFn()). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
ok(
) → Option< T> -
Converts this
Result<T, E>into an Option<T>, discarding the held error value if this is Err. -
or<
F> (Result< T, F> other) → Result<T, F> -
Returns a
Resultvalue as Ok<T, F> if thisResultis Ok<T, E>, otherwise returnsother. -
orElse<
F> (Result< T, F> fn(E)) → Result<T, F> -
Returns a
Resultvalue as Ok<T, F> if thisResultis Ok<T, E>, otherwise callsfnwith the held Err value and returns the returnedResult. -
toString(
) → String -
A string representation of this object.
override
-
transpose(
) → Option< Result< T, E> > -
Available on Result<
Transposes thisOption< , provided by the ResultTranspose extensionT> , E>Result<Option<T>, E>into an Option<Result<T, E>>. -
unwrap(
) → T -
Returns the held value of this
Resultif it is Ok. -
unwrapErr(
) → E -
Returns the held value of this
Resultif it is Err. -
unwrapOr(
T orValue) → T -
Returns the held value of this
Resultif it is Ok, or the given value if thisResultis Err. -
unwrapOrElse(
T elseFn()) → T -
Returns the held value of this
Resultif it is Ok, or returns the returned value fromelseFnif thisResultis Err.
Operators
-
operator ==(
Object other) → bool -
Compare equality between two
Resultvalues.override