map<R> method

ApiResponse<R> map<R>(
  1. R mapper(
    1. T data
    )
)

Map response to another type

Implementation

ApiResponse<R> map<R>(R Function(T data) mapper) {
  if (isSuccess && data != null) {
    try {
      return ApiResponse<R>.success(
        data: mapper(data as T),
        raw: raw,
        statusCode: statusCode,
        headers: headers,
        message: message,
        duration: duration,
      );
    } catch (e) {
      return ApiResponse<R>.error(
        error: ApiException.custom(
          message: 'Failed to map response: ${e.toString()}',
          type: ApiExceptionType.unknown,
        ),
        raw: raw,
        statusCode: statusCode,
        headers: headers,
        duration: duration,
      );
    }
  } else {
    return ApiResponse<R>.error(
      error: error ?? ApiException.custom(
        message: 'No data to map',
        type: ApiExceptionType.unknown,
      ),
      raw: raw,
      statusCode: statusCode,
      headers: headers,
      duration: duration,
    );
  }
}