Result<T, E> class sealed

The result of an operation.

A Result is either successful (Ok) or erroneous (Err).

Examples

The following function tries to parse an integer. Upon success, the resulting value is wrapped in an Ok. If an error occurs during conversion, an Err is returned.

Result<int, String> tryParse(String source) {
  final parsed = int.tryParse(source);

  if (parsed != null) {
    return Ok(parsed);
  }

  return Err('not a number: $source');
}

That function can now be used to try and parse an integer, and return with a helpful error in case of failure.

// prints "Ok(2)"
print(tryParse('2'));

// prints "Err(not a number: two)"
print(tryParse('two'));
Implementers
Available Extensions
Annotations
  • @immutable

Constructors

Result.err(E error)
Creates an Err with the given error.
const
factory
Result.ok(T value)
Creates an Ok with the given value.
const
factory

Properties

err Option<E>
The possibly contained error.
no setter
errorOrNull → E?
The contained error if this is an Err, or null otherwise.
no setter
hashCode int
The hash code for this object.
no setterinherited
isErr bool
Whether this is an Err.
no setter
isOk bool
Whether this is an Ok.
no setter
iterable Iterable<T>
Returns an iterable over the possibly contained value.
no setter
ok Option<T>
The possibly contained value.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
valueOrNull → T?
The contained value if this is an Ok, or null otherwise.
no setter

Methods

and<U>(Result<U, E> other) Result<U, E>
Returns other if this is an Ok, or an Err of the original error otherwise.
andThen<U>(Result<U, E> calculateOther(T value)) Result<U, E>
Returns the result of calculateOther if this is an Ok, or an Err of the original error otherwise.
contains(T value) bool
Returns true if this is an Ok with the given value.
containsErr(E error) bool
Returns true if this is an Err with the given error.
inspect(void inspect(T value)) Result<T, E>
Calls inspect with the contained value if this is an Ok.
inspectErr(void inspect(E error)) Result<T, E>
Calls inspect with the contained error if this is an Err.
isErrAnd(bool condition(E error)) bool
Returns true if this is an Err with a contained error that satisfies condition.
isOkAnd(bool condition(T value)) bool
Returns true if this is an Ok with a contained value that satisfies condition.
map<U>(U map(T value)) Result<U, E>
Transforms the contained value, if any, by applying map to it.
mapErr<F>(F map(E error)) Result<T, F>
Transforms the contained error, if any, by applying map to it.
mapErrOr<F>(F map(E error), F defaultError) → F
Returns the contained error, if any, with map applied to it, or defaultError otherwise.
mapErrOrElse<F>(F map(E error), F calculateDefaultError(T value)) → F
Returns the contained error, if any, with map applied to it, or the result of calculateDefaultError otherwise.
mapOr<U>(U map(T value), U defaultValue) → U
Returns the contained value, if any, with map applied to it, or defaultValue otherwise.
mapOrElse<U>(U map(T value), U calculateDefaultValue(E error)) → U
Returns the contained value, if any, with map applied to it, or the result of calculateDefaultValue otherwise.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
or<F>(Result<T, F> other) Result<T, F>
Returns an Ok of the original value if this is an Ok, or other otherwise.
orElse<F>(Result<T, F> calculateOther(E error)) Result<T, F>
Returns an Ok of the original value if this is an Ok, or the result of calculateOther otherwise.
toString() String
A string representation of this object.
inherited
unwrap({String? msg}) → T
Returns the contained value.
unwrapErr({String? msg}) → E
Returns the contained error.
unwrapErrOr(E defaultError) → E
Returns the contained error, if any, or defaultError otherwise.
unwrapErrOrElse(E calculateDefaultError(T value)) → E
Returns the contained error, if any, or the result of calculateDefaultError otherwise.
unwrapOr(T defaultValue) → T
Returns the contained value, if any, or defaultValue otherwise.
unwrapOrElse(T calculateDefaultValue(E error)) → T
Returns the contained value, if any, or the result of calculateDefaultValue otherwise.

Operators

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

Static Methods

collect<T, E>(Result<T, E> collector(U check<U>(Result<U, E> result))) Result<T, E>
Encloses any number of operations, optionally returning early on Err.
collectAsync<T, E>(FutureOr<Result<T, E>> collector(U check<U>(Result<U, E> result))) Future<Result<T, E>>
Encloses any number of operations, optionally returning early on Err.