map<NewSuccess> method

  1. @optionalTypeArgs
Result<NewSuccess, Failure> map<NewSuccess>(
  1. NewSuccess transform(
    1. Success value
    )
)
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, simply return the failure as-is.

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.

It's most useful for allowing errors to propagate untouched, while processing success values in a pipeline.

Result<Person, ApiFailure> fetchPerson(int id) {
  // ...
}
Result<DateTime, ApiFailure> bigDay = fetchPerson(12).map((person) => person.birthday);

Implementation

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