Result<T extends Object?, E extends Object> class sealed

A type-safe way to handle operations that may fail.

Instead of throwing exceptions or returning null, Result represents either a successful value (Ok) or a failure (Err). This makes error handling explicit and composable.

Use Result when:

  • You need explicit error handling
  • You want to chain operations that might fail
  • You need to distinguish between different error types

Example:

Result<int, String> safeDivide(int a, int b) {
  if (b == 0) return Result.err('Cannot divide by zero');
  return Result.ok(a ~/ b);
}

// Usage:
final result = safeDivide(10, 2);
final value = result.unwrapOr(0); // Get value or default
Implementers
Available extensions
Annotations
  • @immutable

Constructors

Result.err(E error, [StackTrace? stackTrace])
Creates a failed result with the given error.
factory
Result.guardSync(T block())
Executes a synchronous function and wraps the result.
factory
Result.ok(T value)
Creates a successful result with the given value.
const
factory

Properties

err → E?
Returns the error value if this is an Err, otherwise null.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isErr → bool
Returns true if this result is a failure (Err).
no setter
isOk → bool
Returns true if this result is a success (Ok).
no setter
ok → T?
Returns the success value if this is an Ok, otherwise null.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

and(Result<T, E> other) → Result<T, E>
Chains operations: returns this result if it's an error, otherwise other.
andLazy(Result<T, E> otherBlock()) → Result<T, E>
Evaluates otherBlock only when this result is successful.
andThen<U extends Object?>(Result<U, E> otherBlock(T value)) → Result<U, E>
Transforms the success value into a new result, or propagates the error.
clone() → Result<T, E>
Returns a shallow copy of this result.
expect(String message) → T
Unwraps the success value, or throws an exception with message.
expectErr(String message) → E
Unwraps the error value, or throws an exception with message.
flatten() → Result<T, E>

Available on Result<Result<T, E>, E>, provided by the FlattenResultExtension extension

Flattens a nested Result into a single Result.
inspectErr(void block(E error)) → Result<T, E>

Available on Result<T, E>, provided by the InspectResultExtension extension

Executes a side effect if this is an error, then returns this unchanged.
inspectOk(void block(T value)) → Result<T, E>

Available on Result<T, E>, provided by the InspectResultExtension extension

Executes a side effect if this is a success, then returns this unchanged.
isErrAnd(bool predicate(E error)) → bool
Checks if this is an error and the error matches the predicate.
isOkAnd(bool predicate(T value)) → bool
Checks if this is a success and the value matches the predicate.
map<Y extends Object?>(Y block(T value)) → Result<Y, E>
Transforms the success value using block, leaving errors unchanged.
mapErr<F extends Object>(F block(E error)) → Result<T, F>
Transforms the error value using block, leaving success values unchanged.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
or(Result<T, E> other) → Result<T, E>
Returns this result if it's a success, otherwise the already-evaluated other.
orElse<F extends Object>(Result<T, F> otherBlock(E error, StackTrace st)) → Result<T, F>
Returns this success value, or computes a recovery result from the error.
orLazy(Result<T, E> otherBlock()) → Result<T, E>
Evaluates otherBlock only when this result is an error.
toOption() → Option<T>
Converts this result into an Option.
toString() → String
A string representation of this object.
inherited
unwrap() → T
Returns the success value, or throws the error with its original stack trace.
unwrapOr(T defaultValue) → T
Returns the success value, or defaultValue if this is an error.
unwrapOrElse(T block(E error)) → T
Returns the success value, or computes a default from the error.
unwrapOrNull() → T?
Returns the success value as nullable, or null if this is an error.

Operators

operator &(Result<T, E> other) → Result<T, E>
Combines two already-evaluated results using the and operator (&).
operator ==(Object other) → bool
The equality operator.
inherited
operator |(Result<T, E> other) → Result<T, E>
Combines two already-evaluated results using the or operator (|).

Static Methods

guardAsync<T extends Object?, E extends Object>(Future<T> asyncBlock()) → Future<Result<T, E>>
Executes an asynchronous function and wraps the result.
guardExceptionAsync<T extends Object?, E extends Exception>(Future<T> asyncBlock()) → Future<Result<T, E>>
Executes an asynchronous function, catching only Exception types.
guardExceptionSync<T extends Object?, E extends Exception>(T block()) → Result<T, E>
Executes a synchronous function, catching only Exception types.