mapToResult<NewSuccess> method

  1. @optionalTypeArgs
Result<NewSuccess, Failure> mapToResult<NewSuccess>(
  1. Result<NewSuccess, Failure> transform(
    1. Success value
    )
)
inherited

When this Result is a success, transform the value into a new Result. When this Result is a failure, simply return the failure as-is.

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 will always transform to a Result.failure.

Most useful for processing the successful value using another operation which may fail with the same type Failure.

final Result<Person, FormatError> personResult = parsePerson(jsonString);
final Result<DateTime, FormatError> bigDay = personResult.mapToResult(
  (person) => parse(person.birthDateString),
);

Parsing the Person may succeed, but parsing the DateTime may fail. In that case, an initial success is transformed into a failure.

Implementation

@optionalTypeArgs
Result<NewSuccess, Failure> mapToResult<NewSuccess>(
  Result<NewSuccess, Failure> Function(Success value) transform,
) =>
    when(
      success: (value) => transform(value),
      failure: (error) => Result.failure(error),
    );