mapError<NewFailure extends Object> method

  1. @optionalTypeArgs
Result<Success, NewFailure> mapError<NewFailure extends Object>(
  1. NewFailure transform(
    1. Failure error
    )
)
inherited

When this Result is a success, simply return the value as-is. 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.

Most useful for turning raw Errors into Freezed Unions for describing failures.

Result<Person, ApiError> apiPerson(int id) {
  final Result<Person, DioError> raw = await dioGetApiPerson(12);
  return raw.mapError((error) => _interpretDioError(error));
}

Implementation

@optionalTypeArgs
Result<Success, NewFailure> mapError<NewFailure extends Object>(
  NewFailure Function(Failure error) transform,
) =>
    when(
      success: (value) => Result.success(value),
      failure: (error) => Result.failure(transform(error)),
    );