httpMultiPart<T> method

Future<ApiResponse<T>> httpMultiPart<T>({
  1. required Function converter,
  2. List<MultipartFile>? files,
  3. Map<String, String>? fields,
  4. bool isAuth = true,
  5. String method = "POST",
})

Implementation

Future<ApiResponse<T>> httpMultiPart<T>(
    {required Function converter,List<http.MultipartFile>? files,Map<String,String>? fields, bool isAuth = true, String method = "POST"}) async {
  var url = createUri("${HttpX.instance.baseUrl}$this");
  var request = http.MultipartRequest((method == "PUT") ? "PUT" : "POST", url)
    ..headers.addAll({
      if (isAuth) HttpX.instance.headerName:  await HttpX.instance.token(),
      "content-type": "multipart/form-data"
    });
  if(files!=null)request.files.addAll(files);
  if(fields!=null)request.fields.addAll(fields);

  try {
    http.Response response = await http.Response.fromStream(await request.send());
    return ApiResponse<T>(
        code: response.statusCode,
        data: converter(jsonDecode(response.body)),
        error: null);
  } catch (error) {
    return ApiResponse<T>(
        code: HttpStatus.internalServerError,
        data: null,
        error: error.toString());
  }
}