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
Implementers

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 onNull if null.
factory
Result.fromPredicate(R value, bool predicate(R r), L onFalse(R r))
Creates a Success if predicate returns true for value, otherwise creates a Failure using onFalse.
factory
Result.success(R r)
Creates a Success containing the success value r.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isFailure bool
Returns true if this is a Failure (failure).
no setter
isSuccess bool
Returns true if this is a Success (success).
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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 onFailure if this is Failure, or onSuccess if this is Success.
getFailureOrNull() → L?
Returns the Failure value or null if this is Success.
getOrElse(R defaultValue(L l)) → R
Returns the Success value, or defaultValue if this is Failure.
getOrNull() → R?
Returns the Success value or null if 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 action if this is Success, then returns this unchanged.
tapFailure(void action(L l)) Result<L, R>
Executes action if 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<L, R>> results) Result<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<L, R>> result) Result<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 run and wraps the result in a Success. If run throws, catches the exception and wraps it in Failure via onError.
guardAsync<L, R>(Future<R> run(), L onError(Object error, StackTrace stack)) Future<Result<L, R>>
Async version of guard. Executes run and wraps the result in Success. If run throws, catches the exception and wraps it in Failure via onError.
waitAll<L, R>(List<Future<Result<L, R>>> futures) Future<Result<L, List<R>>>
Async version of combine. Awaits all futures and combines results.