Result<T, E> class

The Result Monad type which will encapsulate a success (ok) value of type T or a failure (error) value of type E

Constructors

Result.error(E error)
Create a new Result of the expected error type with the error value Result<int,String> getErrorValue() => Result.error('This is an error');
Result.ok(T success)
Create a new Result of the expected success type with the success value Result<int,String> getSuccess() => Result.ok(10);

Properties

error → E
Returns the error if it is a failure Result Monad otherwise throws ResultMonadException. It is best to check that it is a failure monad by calling isFailure.
no setter
hashCode int
The hash code for this object.
no setterinherited
isFailure bool
Returns true if the Result Monad has a failure value, false otherwise
no setter
isSuccess bool
Returns true if the Result Monad has a success value, false otherwise
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value → T
Returns the value if it is a success Result Monad otherwise throws ResultMonadException. It is best to check that it is a success monad by calling isSuccess.
no setter

Methods

andThen<T2, E2>(Result<T2, E2> thenFunction(T)) Result<T2, dynamic>
Executes the anonymous function passing the current value to it to support operation chaining.
andThenAsync<T2, E2>(FutureResult<T2, E2> thenFunction(T)) FutureResult<T2, dynamic>
Executes the anonymous async function passed in on the current value. Due to Dart syntax peculiarities it may be cleaner to work with interim results.
andThenSuccess<T2, E2>(T2 thenFunction(T)) Result<T2, dynamic>
NOTE: This is old syntax preserved for backward compatibility. Instead use the transform method
andThenSuccessAsync<T2, E2>(Future<T2> thenFunction(T)) FutureResult<T2, dynamic>
NOTE: This is old syntax preserved for backward compatibility. Instead use the transformAsync method
errorCast<T2>() Result<T2, E>
Maps from an error monad with one return type to another. This is for cases where you know you got an error result of the same type that you want to propagate up but whose success type is different.
fold<T2>({required T2 onSuccess(T value), required T2 onError(E error)}) → T2
Maps (folds) the result into a new type consistent across both the success and failure conditions.
getErrorOrElse(E orElse()) → E
Returns the error value if the monad is wrapping a failure or return the specified default value.
getValueOrElse(T orElse()) → T
Returns the success value if the monad is wrapping a success or return the specified default value.
mapError<E2>(E2 mapFunction(E error)) Result<T, E2>
Maps from a monad from one error type to another. This is useful for transforming from error mappings between APIs etc.
mapValue<T2>(T2 mapFunction(T value)) Result<T2, E>
Maps from a monad from one result type to another. This is useful for transforming from error mappings between APIs etc.
match({required dynamic onSuccess(T value), required dynamic onError(E error)}) → void
A mechanism for executing functions on a result monad for each of the two conditions result.match( onSuccess: (value) => log.finest('Result: $value'); onError: (error) => log.severe('Error getting result: $error'); )
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
transform<T2, E2>(T2 thenFunction(T)) Result<T2, dynamic>
Executes the anonymous function passing the current value to it to support operation chaining. The function returns a non-Result value therefore this assumes that the operation is always a success. This is helpful for chaining functions that don't have result monads.
transformAsync<T2, E2>(Future<T2> thenFunction(T)) FutureResult<T2, dynamic>
Executes the anonymous async function passed in on the current value. Due to Dart syntax peculiarities it may be cleaner to work with interim results.The function returns a non-Result value therefore
withError(dynamic withFunction(E)) Result<T, dynamic>
Executes the anonymous function passing the current error value to it to support operation chaining but as a pass-through process. The original result object is automatically passed on to the next chain unchanged nor is it possible to pass on a new result type like with the "andThen" methods. This is generally intended to be used at the end of a chain for reporting errors but can be used anywhere within it. If an error result hasn't been generated by that time then this won't do anything.
withErrorAsync(Future<void> withFunction(E)) FutureResult<T, dynamic>
Executes the anonymous function passing the current error value to it to support operation chaining but as a pass-through process. The original result object is automatically passed on to the next chain unchanged nor is it possible to pass on a new result type like with the "andThen" methods. This is generally intended to be used at the end of a chain for reporting errors but can be used anywhere within it. If an error result hasn't been generated by that time then this won't do anything.
withResult(dynamic withFunction(T)) Result<T, dynamic>
Executes the anonymous function passing the current success value to it to support operation chaining but as a pass-through process. The original result object is automatically passed on to the next chain unchanged nor is it possible to pass on a new result type like with the "andThen" methods.
withResultAsync(Future<void> withFunction(T)) FutureResult<T, dynamic>
Executes the anonymous function passing the current success value to it to support operation chaining but as a pass-through process. The original result object is automatically passed on to the next chain unchanged nor is it possible to pass on a new result type like with the "andThen" methods.

Operators

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