Err<T, E> class final

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');
  });
}
Inheritance

Constructors

Err(E e)
Result is a type that that represents either success (Ok) or failure (Err)
const

Properties

e → E
Err wrapped error
final
hashCode int
The hash code for this object.
no setteroverride
isErr bool
Resturns true if the result is Err (failure)
no setteroverride
isOk bool
Resturns true if the result is Ok (success)
no setteroverride
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.
override
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.
override
contains(Object? x) bool
Returns true if the result is an Ok (success) value containing the given value.
override
containsErr(Object? x) bool
Returns true if the result is an Err (failure) value containing the given value.
override
err() → E?
Converts from Result<T, E> to E?.
override
expect(String message) → T
Returns the contained Ok (success) value.
override
expectErr(String message) → E
Returns the contained Err (failure) value.
override
inspect(void f(T value)) Result<T, E>
Calls the provided closure with the contained value (if Ok)
override
inspectErr(void f(E error)) Result<T, E>
Calls the provided closure with the contained error (if Err).
override
isErrAnd(bool f(E error)) bool
Returns true if the result is Err and the value matches the predicate f
override
isOkAnd(bool f(T value)) bool
Returns true if the result is Ok and the value matches the predicate f
override
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.
override
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.
override
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)).
override
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.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
ok() → T?
Converts from Result<T, E> to T?.
override
or<F>(Result<T, F> res) Result<T, F>
Returns res if the result is Err, otherwise returns the Ok (success) value of this.
override
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.
override
toString() String
A string representation of this object.
override
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`.
override
unwrapErr() → E
Returns the contained Err (failure) value.
override
unwrapOr(T fallback) → T
Returns the contained Ok (success) value or a provided fallback.
override
unwrapOrElse(T fallback(E error)) → T
Returns the contained Ok (success) value or computes it from the closure.
override
when<U>({required U ok(T value), required U err(E error)}) → U
Maps a Result<T, E> to U
override

Operators

operator ==(Object? other) bool
The equality operator.
override