Ok<T extends Object?, E extends Object> class final

Represents a successful result containing a value.

This is used when an operation succeeds and produces a meaningful value.

Example:

Result<int, String> parseNumber(String input) {
  final value = int.tryParse(input);
  if (value != null) return Ok(value);
  return Err('Invalid number');
}
Inheritance
Available extensions
Annotations
  • @immutable

Constructors

Ok(T value)
Creates a successful result with the given value.
const

Properties

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

Methods

and(Result<T, E> other) Result<T, E>
Chains operations: returns this result if it's an error, otherwise other.
inherited
andLazy(Result<T, E> otherBlock()) Result<T, E>
Evaluates otherBlock only when this result is successful.
inherited
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.
inherited
clone() Result<T, E>
Returns a shallow copy of this result.
inherited
expect(String message) → T
Unwraps the success value, or throws an exception with message.
inherited
expectErr(String message) → E
Unwraps the error value, or throws an exception with message.
inherited
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.
inherited
isOkAnd(bool predicate(T value)) bool
Checks if this is a success and the value matches the predicate.
inherited
map<Y extends Object?>(Y block(T value)) Result<Y, E>
Transforms the success value using block, leaving errors unchanged.
inherited
mapErr<F extends Object>(F block(E error)) Result<T, F>
Transforms the error value using block, leaving success values unchanged.
inherited
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.
inherited
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.
inherited
orLazy(Result<T, E> otherBlock()) Result<T, E>
Evaluates otherBlock only when this result is an error.
inherited
toOption() Option<T>
Converts this result into an Option.
inherited
toString() String
A string representation of this object.
override
unwrap() → T
Returns the success value, or throws the error with its original stack trace.
inherited
unwrapOr(T defaultValue) → T
Returns the success value, or defaultValue if this is an error.
inherited
unwrapOrElse(T block(E error)) → T
Returns the success value, or computes a default from the error.
inherited
unwrapOrNull() → T?
Returns the success value as nullable, or null if this is an error.
inherited

Operators

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