Result<L, R> class
sealed
Core Result monad — the heart of functional error handling.
Result is a sealed class representing a value that is either:
- Success — a successful computation containing a value of type
R. - Failure — a failed computation containing an error of type
L.
Using sealed classes ensures exhaustive pattern matching at compile time, forcing developers to handle both success and failure cases.
Why Result over Exceptions?
Exceptions break the type system — a function signature like int parse(String s)
tells you nothing about what can go wrong. With Result, the return type
Result<FormatError, int> explicitly communicates all possible outcomes.
Basic Usage
Result<String, int> divide(int a, int b) {
if (b == 0) return Failure('Division by zero');
return Success(a ~/ b);
}
final result = divide(10, 2);
// Exhaustive pattern matching — compiler ensures both cases are handled
final output = switch (result) {
Failure(value: final error) => 'Error: $error',
Success(value: final data) => 'Result: $data',
};
Functional Chaining
final result = Success<String, int>(10)
.map((x) => x * 2) // Success(20)
.flatMap((x) => divide(x, 4)) // Success(5)
.mapFailure((e) => 'Failed: $e'); // Only transforms if Failure
Constructors
- Result.failure(L l)
-
Creates a Failure containing the failure value
l.factory - Result.fromNullable(R? value, L onNull())
-
Creates a Success from a non-null value, or Failure from
onNullif null.factory - Result.fromPredicate(R value, bool predicate(R r), L onFalse(R r))
-
Creates a Success if
predicatereturns true forvalue, otherwise creates a Failure usingonFalse.factory - Result.success(R r)
-
Creates a Success containing the success value
r.factory
Properties
Methods
-
flatMap<
R2> (Result< L, R2> f(R r)) → Result<L, R2> - Chains a computation that returns a Result, avoiding nested Results.
-
fold<
W> (W onFailure(L l), W onSuccess(R r)) → W -
Applies
onFailureif this is Failure, oronSuccessif this is Success. -
getFailureOrNull(
) → L? -
Returns the Failure value or
nullif this is Success. -
getOrElse(
R defaultValue(L l)) → R -
Returns the Success value, or
defaultValueif this is Failure. -
getOrNull(
) → R? -
Returns the Success value or
nullif this is Failure. -
map<
R2> (R2 f(R r)) → Result< L, R2> -
Transforms the Success value using
f, leaving Failure unchanged. -
mapFailure<
L2> (L2 f(L l)) → Result< L2, R> -
Transforms the Failure value using
f, leaving Success unchanged. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
swap(
) → Result< R, L> - Swaps Failure and Success types.
-
tap(
void action(R r)) → Result< L, R> -
Executes
actionif this is Success, then returns this unchanged. -
tapFailure(
void action(L l)) → Result< L, R> -
Executes
actionif this is Failure, then returns this unchanged. -
toOption(
) → Option< R> - Converts this Result to an Option, discarding the Failure value.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
combine<
L, R> (List< Result< results) → Result<L, R> >L, List< R> > - Combines a list of Results into a single Result containing a list of all success values.
-
flatten<
L, R> (Result< L, Result< result) → Result<L, R> >L, R> - Flattens a nested Result into a single Result.
-
guard<
L, R> (R run(), L onError(Object error, StackTrace stack)) → Result< L, R> -
Executes
runand wraps the result in a Success. Ifrunthrows, catches the exception and wraps it in Failure viaonError. -
guardAsync<
L, R> (Future< R> run(), L onError(Object error, StackTrace stack)) → Future<Result< L, R> > -
Async version of guard. Executes
runand wraps the result in Success. Ifrunthrows, catches the exception and wraps it in Failure viaonError. -
waitAll<
L, R> (List< Future< futures) → Future<Result< >L, R> >Result< L, List< >R> > - Async version of combine. Awaits all futures and combines results.