Err<T, E> class

❌ Represents a failed operation with an error of type E.

ALWAYS use Err() to wrap errors instead of throwing exceptions!

This is one of the two concrete implementations of Result, used to wrap error values and propagate them through a chain of operations.

🚨 Best Practices:

  • Use const Err(error) when possible for better performance
  • Always pair with Ok() for complete error handling
  • Never throw exceptions - use Err() instead
  • Handle errors explicitly with when() method

βœ… Correct Usage:

// βœ… GOOD: Use Err() for error results
Result<User, String> fetchUser(String id) {
  if (id.isEmpty) {
    return Err('Invalid ID provided');
  }
  // Continue with fetching logic...
  return Ok(user);
}

// βœ… GOOD: Use const when possible
const result = Err('validation failed');

// βœ… GOOD: Handle errors with when()
fetchUser(userId).when(
  ok: (user) => displayUserProfile(user),
  err: (error) => showErrorMessage(error),
);

❌ Avoid These Patterns:

// ❌ BAD: Never throw exceptions
Result<User, String> badFetch(String id) {
  if (id.isEmpty) throw Exception('Invalid ID'); // Don't do this!
}

// ❌ BAD: Don't ignore error handling
fetchUser(id).when(
  ok: (user) => processUser(user),
  err: (error) => {}, // Empty error handler
);
Inheritance
Available extensions

Constructors

Err(E error)
Creates a new error result containing error

Properties

data β†’ T
Gets the success value or throws if this is an error
no setterinherited
error β†’ E
The error 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>
Short-circuits a chain of Result operations when an error occurs
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>
Preserves the error while maintaining 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 err to the contained error
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