Result<S, E> class sealed

A sealed class representing the result of an operation that can either succeed or fail.

Result is a type-safe way to handle operations that might fail without using exceptions. It can be either a Success containing data of type S, or an Error containing an error of type E.

This follows the Railway-oriented programming pattern and is similar to Rust's Result type.

Example:

Result<String, String> divide(int a, int b) {
  if (b == 0) {
    return Error('Division by zero');
  }
  return Success((a / b).toString());
}
Implementers

Properties

hashCode int
The hash code for this object.
no setterinherited
isError bool
Returns true if this result represents a failed operation.
no setter
isSuccess bool
Returns true if this result represents a successful operation.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

execute() → void
Forces evaluation of this result without returning any value.
flat() → (S?, E?)
Flattens this result into a tuple representation.
getOrNull() → S?
Extracts the success value or returns null if this result is an error.
getOrThrow() → S
Extracts the success value or throws an exception if this result is an error.
mapError<E2>(E2 mapper(E err)) Result<S, E2>
Transforms the error value using a mapper function.
mapOnError<E2>(Result<S, E2> mapper(E err)) Result<S, E2>
Transforms the error value using a mapper that returns a Result.
mapOnSuccess<S2>(Result<S2, E> mapper(S data)) Result<S2, E>
Transforms the success value using a mapper that returns a Result.
mapSuccess<S2>(S2 mapper(S data)) Result<S2, E>
Transforms the success value using a mapper function.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
nullable() Result<S?, E>
Converts this result to allow nullable success values.
onError(void onError(E error)) Result<S, E>
Executes a side effect if this result is an error.
onSuccess(void onSuccess(S data)) Result<S, E>
Executes a side effect if this result is successful.
resolve({required S onError(E error)}) Success<S, E>
Converts any error result into a successful result using a recovery function.
toString() String
A string representation of this object.
inherited
when<T>({required T success(S data), required T error(E error)}) → T
Pattern matches on this result and executes the appropriate callback.

Operators

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

Static Methods

error<S, E>(E error) Result<S, E>
Creates an error result containing the given error.
future<S>(Future<S> ftr) Future<Result<S, dynamic>>
Awaits a Future and wraps its result or any thrown exception in a Result.
handle<S>(S fn()) Result<S, dynamic>
Executes a function and wraps its result or any thrown exception in a Result.
success<S, E>(S data) Result<S, E>
Creates a successful result containing the given data.