Failure<S, E extends Exception> class
final
Represents a failed outcome of an operation, containing an error of type E
.
This class extends Result and signifies that an operation has failed,
yielding an error of type E
. The associated success type S
is still specified
to maintain consistency with the Result interface.
Type Parameters:
S
: The type of the successful result (not used in this class but required for consistency).E
: The type of the error, extending Exception.
Example usage with different error types:
// Example 1: Using Failure with a generic Exception
final result = Failure<int, Exception>(Exception('An error occurred'));
print(result.error); // Output: Exception: An error occurred
// Example 2: Using Failure with a FormatException
final result = Failure<String, FormatException>(FormatException('Invalid format'));
print(result.error); // Output: FormatException: Invalid format
// Example 3: Using Failure with a custom DatabaseException
final result = Failure<List<double>, DatabaseException>(DatabaseException('Database error'));
print(result.error.message); // Output: Database error
// Example 4: Using Failure with a TimeoutException
final result = Failure<User, TimeoutException>(TimeoutException('Operation timed out'));
print(result.error.message); // Output: Operation timed out
Constructors
- Failure.new(E error)
-
const
Properties
- error → E
-
The error resulting from a failed operation.
final
- errorOrNull → E?
-
Returns the error if this is a Failure, otherwise returns
null
.no setterinherited - hashCode → int
-
The hash code for this object.
no setterinherited
- isFailure → bool
-
Returns true if this result is a failure.
no setterinherited
- isSuccess → bool
-
Returns true if this result is a success.
no setterinherited
-
props
→ List<
Object?> -
The list of properties that will be used to determine whether
two instances are equal.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stringify → bool?
-
If set to
true
, thetoString
method will be overridden to output this instance'sprops
.no setterinherited - valueOrNull → S?
-
Returns the success value if this is a Success, otherwise returns
null
.no setterinherited
Methods
-
fold<
T> (T onSuccess(S value), T onFailure(E error)) → T -
Reduces this Result into a single value.
If the result is a Success, the
onSuccess
function is applied to the value. If it is a Failure, theonFailure
function is applied to the error.override -
getOrElse(
S defaultValue) → S -
Returns the success value if available; otherwise, returns the provided
defaultValue
.override -
getOrThrow(
) → S -
Returns the success value if this Result is a Success.
If this is a Failure, it throws the associated error.
override
-
map<
T> (T mapper(S value)) → Result< T, E> -
Transforms the success value of type
S
using the providedmapper
function. If the result is a Success, the mapper is applied; if it's a Failure, the error is preserved.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String - A string representation of this object.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited