Ok<T, E> class

βœ… Represents a successful operation with a value of type T.

ALWAYS use Ok() to wrap successful values instead of returning null!

This is one of the two concrete implementations of Result, used to wrap successful values and allow them to be processed in a functional way.

🚨 Best Practices:

  • Use const Ok(value) when possible for better performance
  • Always pair with Err() for complete error handling
  • Never return null - use Ok() or Err() instead

βœ… Correct Usage:

// βœ… GOOD: Use Ok() for successful results
Result<int, String> parseInt(String input) {
  try {
    return Ok(int.parse(input));
  } catch (e) {
    return Err('Could not parse "$input" to integer');
  }
}

// βœ… GOOD: Use const when possible
const result = Ok('success');

// βœ… GOOD: Handle with when()
parseInt('42').when(
  ok: (value) => print('Got number: $value'),
  err: (msg) => print('Error: $msg'),
);

❌ Avoid These Patterns:

// ❌ BAD: Never return null
Result<int, String>? badFunction() => null;

// ❌ BAD: Don't access .data directly without checking
final value = result.data; // Can throw!
Inheritance
Available extensions

Constructors

Ok(T data)
Creates a new successful result containing data

Properties

data β†’ T
The successful value contained in this result
final
errorOrNull β†’ E?
Gets the error value if present, null otherwise
no setterinherited
hashCode β†’ int
The hash code for this object.
no setterinherited
isErr β†’ bool
Returns true if this is an error result
no setterinherited
isOk β†’ bool
Returns true if this is a success result
no setterinherited
runtimeType β†’ Type
A representation of the runtime type of the object.
no setterinherited

Methods

filter(bool predicate(T item)) β†’ Result<List<T>, E>

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

Filters elements in a successful list result
flatMap<R>(Result<R, E> ok(T value), [Result<R, E> err(E error)?]) β†’ Result<R, E>
Chains another Result-returning operation based on the success value
override
getOrDefault(T defaultValue) β†’ T

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

Gets the success value or falls back to a default value
getOrElse(T orElse(E error)) β†’ T

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

Gets the success value or extracts a value from the error
map<R>(R transform(T value), [E err(E error)?]) β†’ Result<R, E>
Transforms the success value using transform while preserving the Result structure
override
mapEach<R>(R transform(T item)) β†’ Result<List<R>, E>

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

Maps each element in a successful list result
mapError<F>(F transform(E error)) β†’ Result<T, F>

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

Transforms the error value if present
noSuchMethod(Invocation invocation) β†’ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
recover(Result<T, E> transform(E error)) β†’ Result<T, E>

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

Attempts to recover from an error
toString() β†’ String
A string representation of this object.
inherited
when<R>({required R ok(T), required R err(E)}) β†’ R
Processes this result by applying ok to the contained value
override
whenData<R>(R ok(T)) β†’ R
Accesses the success value directly, throwing an error if the Result is an Err.
inherited
whenError<R>(R err(E)) β†’ R?
Executes a function only if the Result contains an error.
inherited

Operators

operator ==(Object other) β†’ bool
The equality operator.
inherited