send<T> method Null safety

Future<T> send<T>(
  1. ApiRequest request
)

Implementation

Future<T> send<T>(ApiRequest request) async {
  final url = '/${request.version}/${request.url}';
  //inject current user id if field is empty when only needs
  //need to clarify the condition
  if (request.method != Method.get &&
      request.body['user_id'] == null &&
      request.userId != null) {
    request.body['user_id'] = request.userId; // ?? state.userId;
  }

  Map<String, dynamic> json;

  switch (request.method) {
    case Method.get:
      json = await client.get(
        url: url,
        queryParams: request.queryParams,
        headers: request.headers,
      );
      break;
    case Method.post:
      if (!request.isMultipart) {
        json = await client.post(
          url: url,
          queryParams: request.queryParams,
          body: request.body,
          headers: request.headers,
        );
      } else {
        json = await client.multipartRequest(
          method: request.method,
          url: url,
          body: request.body,
          queryParams: request.queryParams,
          headers: request.headers,
          progress: request.progress,
        );
      }
      break;
    case Method.delete:
      json = await client.delete(
        url: url,
        queryParams: request.queryParams,
        body: request.body,
        headers: request.headers,
      );
      break;
    case Method.patch:
      json = await client.patch(
        url: url,
        queryParams: request.queryParams,
        body: request.body,
        headers: request.headers,
      );
      break;
    case Method.put:
      if (!request.isMultipart) {
        json = await client.put(
          url: url,
          queryParams: request.queryParams,
          body: request.body,
          headers: request.headers,
        );
      } else {
        json = await client.multipartRequest(
          method: request.method,
          url: url,
          body: request.body,
          queryParams: request.queryParams,
          headers: request.headers,
          progress: request.progress,
        );
      }
  }

  final res = await request.response(json);
  return res as T;
}