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- useOk()orErr()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<
Filters elements in a successful list resultList< , provided by the ResultCollectionExtensions extensionT> , E> -
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<
Gets the success value or falls back to a default valueT, E> , provided by the ResultExtensions extension -
getOrElse(
T orElse(E error)) β T -
Available on Result<
Gets the success value or extracts a value from the errorT, E> , provided by the ResultExtensions extension -
map<
R> (R transform(T value), [E err(E error)?]) β Result< R, E> -
Transforms the success value using
transformwhile preserving the Result structureoverride -
mapEach<
R> (R transform(T item)) β Result< List< R> , E> -
Available on Result<
Maps each element in a successful list resultList< , provided by the ResultCollectionExtensions extensionT> , E> -
mapError<
F> (F transform(E error)) β Result< T, F> -
Available on Result<
Transforms the error value if presentT, E> , provided by the ResultExtensions extension -
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<
Attempts to recover from an errorT, E> , provided by the ResultExtensions extension -
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
okto the contained valueoverride -
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