Result<T> class

A discriminated union that encapsulates a successful outcome with a value of type T or a failure with an arbitrary error and stack trace.

For encapsulating a value:

final result = Result.success('success o/');

For encapsulating an error and a stackTrace:

 Result<int>.failure('failure :/', stackTrace);

Constructors

Result.failure(Object error, [StackTrace? stackTrace])
Returns an instance that encapsulated the Error and StackTrace as Failure.
factory
Result.success(T value)
Returns an instance that encapsulated the given value as successful value.
factory

Properties

hashCode int
The hash code for this object.
no setteroverride
isFailure bool
Returns true if this instance represents a failed outcome. In this case isSuccess returns false.
no setter
isSuccess bool
Returns true if this instance represents a successful outcome. In this case isFailure returns false.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

failureOrNull() → Failure?
Returns the encapsulated Failure exception if this instance represents Result.isFailure or null if it is Result.isSuccess.
fold<R>({required R onSuccess(T value), required R onFailure(Failure failure)}) → R
Returns the result of onSuccess for the encapsulated value if this instance represents Result.isSuccess or the result of onFailure function for the encapsulated error and stackTrace in Failure if it is Result.isFailure.
getOrDefault(T defaultValue) → T
Returns the encapsulated value if this instance represents Result.isSuccess or the defaultValue if it is Result.isFailure.
getOrElse({required T onFailure(Failure failure)}) → T
Returns the encapsulated value if this instance represents Result.isSuccess or the result of onFailure function for the encapsulated Failure exception if it is Result.isFailure.
getOrNull() → T?
Returns the encapsulated value if this instance represents Result.isSuccess or null if it is Result.isFailure.
getOrThrow() → T?
Returns the encapsulated value if this instance represents Result.isSuccess or throws the encapsulated in Failure exception if it is Result.isFailure.
map<R>({required R transform(T value)}) Result<R>
Returns the encapsulated result of the given transform functions applied to the encapsulated value if this instance represents Result.isSuccess or the original encapsulated error and stackTrace in Failure if it is Result.isFailure.
mapCatching<R>({required R transform(T value), bool test(Object error)?}) Result<R>
Returns the encapsulated result of the given transform function applied to the encapsulated value if this instance represents Result.isSuccess or the original encapsulated errorandstackTrace` in Failure if it is Result.isFailure.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onFailure({required void action(Failure failure)}) Result<T>
Performs the given action on the encapsulated error and stackTrace in Failure if this instance represents Result.isFailure Returns the original Result unchanged.
onSuccess({required void action(T value)}) Result<T>
Performs the given action on the encapsulated value if this instance represents Result.isSuccess. Returns the original Result unchanged.
recover({required T transform(Failure failure)}) Result<T>
Returns the encapsulated result of the given transform function applied to the encapsulated error and stackTrace in Failure if this instance represents Result.isFailure or the original encapsulated value if it is Result.isSuccess.
recoverCatching({required T transform(Failure failure), bool test(Object error)?}) Result<T>
Returns the encapsulated result of the given transform function applied to the encapsulated error and stackTrace in Failure if this instance represents Result.isFailure or the original encapsulated value if it is Result.isSuccess.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override