mapToResultWhen<NewSuccess, NewFailure extends Object> method

  1. @optionalTypeArgs
Result<NewSuccess, NewFailure> mapToResultWhen<NewSuccess, NewFailure extends Object>({
  1. required Result<NewSuccess, NewFailure> success(
    1. Success value
    ),
  2. required Result<NewSuccess, NewFailure> failure(
    1. Failure error
    ),
})
inherited

When this Result is a success, transform the value into a new Result. When this Result is a failure, transform the error into a new Result.

After the transformation, isSuccess may be different between the new and old Results—the transform IS allowed to change the nature of the outcome.

A Result.success may transform to either a Result.success or a Result.failure.

A Result.failure may transform to either a Result.success or a Result.failure.

Both transforms must agree on the NewSuccess and NewFailure types.

Uncommon, but allows a single step of the pipeline to both try another operation on a success value which may fail in a new way with a new error type, and to translate any original error into the new type of Failure.

Result<Person, DioError> fetchPerson(int id) {
  // ...
}
Result<String, ProcessingError> fullName = fetchPerson(12).mapToResultWhen(
    success: (person) => _fullName(person.firstName, person,lastName),
    failure: (dioError) => _asProcessingError(dioError),
);

Implementation

@optionalTypeArgs
Result<NewSuccess, NewFailure>
    mapToResultWhen<NewSuccess, NewFailure extends Object>({
  required Result<NewSuccess, NewFailure> Function(Success value) success,
  required Result<NewSuccess, NewFailure> Function(Failure error) failure,
}) =>
        when(
          success: (value) => success(value),
          failure: (error) => failure(error),
        );