Result<T> class sealed

The outcome of an operation that can fail: either an Ok carrying a value, or an Err carrying a Failure.

Returning a Result keeps failures in the type signature instead of in the control flow, so a caller cannot forget that an operation can fail. The type is sealed, so a switch over it is exhaustive:

final result = await repository.getUser();
switch (result) {
  case Ok(:final value):
    print(value.name);
  case Err(:final failure):
    log(failure.message);
}
Implementers

Properties

failureOrNull Failure?
The failure when this is an Err, or null when it is an Ok.
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
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
valueOrNull → T?
The value when this is an Ok, or null when it is an Err.
no setter

Methods

flatMap<R>(Result<R> transform(T value)) Result<R>
Chains another fallible operation onto an Ok, passing an Err through untouched.
fold<R>({required R onOk(T value), required R onErr(Failure failure)}) → R
Collapses both branches into a single value of type R.
getOrElse(T fallback) → T
The value when this is an Ok, otherwise fallback.
map<R>(R transform(T value)) Result<R>
Applies transform to the value of an Ok, passing an Err through untouched.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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