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<
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> -
Short-circuits a chain of Result operations when an error occurs
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> -
Preserves the error while maintaining the Result structure
override
-
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
errto the contained erroroverride -
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