mapWhen<NewSuccess, NewFailure extends Object> method

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

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

After the transformation, isSuccess will be identical between the new and old Results—the transform doesn't change the nature of the outcome, only the type.

A Result.success will always transform to a Result.success.

A Result.failure will always transform to a Result.failure.

Uncommon, but allows a single step of the pipeline to change both the values and the errors.

Result<Person, DioError> fetchPerson(int id) {
  // ...
}
Result<String, ApiFailure> fullName = fetchPerson(12).mapWhen(
    success: (person) => _sanitize(person.firstName, person,lastName),
    failure: (error) => _interpretDioError(error),
);

Implementation

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