Result<T, E> class sealed

Result of an operation that can either succeed with T or fail with E.

Use Ok for successful values and Err for failures. Chain validation or conversion steps with andThen, and recover failures with orElse.

Result<int, String> parseCount(String text) {
  final value = int.tryParse(text);
  return value == null ? const Err('invalid count') : Ok(value);
}

Result<int, String> requirePositive(int value) {
  return value > 0 ? Ok(value) : const Err('count must be positive');
}

final count = parseCount('42').andThen(requirePositive);
Available extensions

Properties

hashCode → int
The hash code for this object.
no setterinherited
isErr → bool
Whether this result is Err.
no setter
isOk → bool
Whether this result is Ok.
no setter
orReject → T

Available on Result<T, Rejection>, provided by the RejectOnFailure extension

The extracted value, or a thrown Rejection.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

andThen<R>(Result<R, E> next(T value)) → Result<R, E>
Chains another result-producing operation when this result is successful.
map<R>(R mapper(T value)) → Result<R, E>
Maps a successful value and leaves errors unchanged.
mapErr<F>(F mapper(E error)) → Result<T, F>
Maps an error value and leaves successful values unchanged.
match<R>({required R ok(T value), required R err(E error)}) → R
Pattern matches this result.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
orElse<F>(Result<T, F> next(E error)) → Result<T, F>
Chains another result-producing operation when this result failed.
toString() → String
A string representation of this object.
inherited
unwrapOr(T fallback) → T
Returns the successful value, or fallback when this result failed.
unwrapOrElse(T fallback(E error)) → T
Returns the successful value, or computes one from the error.

Operators

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