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');
  });
}
Implementers
Annotations
  • @immutable

Constructors

Result()
const

Properties

hashCode int
The hash code for this object.
no setterinherited
isErr bool
Resturns true if the result is Err (failure)
no setter
isOk bool
Resturns true if the result is Ok (success)
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

and<U>(Result<U, E> res) Result<U, E>
Return res if the result is Ok, otherwise returns the Err value of this.
andThen<U>(Result<U, E> okMap(T value)) Result<U, E>
Calls op if the result is Ok, otherwise returns the Err value of this.
contains(Object? x) bool
Returns true if the result is an Ok (success) value containing the given value.
containsErr(Object? x) bool
Returns true if the result is an Err (failure) value containing the given value.
err() → E?
Converts from Result<T, E> to E?.
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 is Err and the value matches the predicate f
isOkAnd(bool f(T value)) bool
Returns true if the result is Ok and the value matches the predicate f
map<U>(U okMap(T value)) Result<U, E>
Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok (success) value, and leaving an Err (failure) value untouched.
mapErr<F>(F errMap(E error)) Result<T, F>
Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err (failure) value, leaving an Ok (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 (if Ok (success)).
mapOrElse<U>({required U errMap(E error), required U okMap(T value)}) → U
Maps a Result<T, E> to U by applying fallback function fallback to a contained Err (failure) value, or function op to a contained Ok (success) value.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
ok() → T?
Converts from Result<T, E> to T?.
or<F>(Result<T, F> res) Result<T, F>
Returns res if the result is Err, otherwise returns the Ok (success) value of this.
orElse<F>(Result<T, F> errMap(E error)) Result<T, F>
Calls op if the result is Err, otherwise returns the Ok value of this.
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 use switch statements with the Result.type and handle the Err (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> to U

Operators

operator ==(Object other) bool
The equality operator.
inherited