postMultipartData method

Future<Response> postMultipartData(
  1. String uri,
  2. Map<String, String> body,
  3. List<MultipartBody> multipartBody, {
  4. Map<String, String>? headers,
})

Implementation

Future<Response> postMultipartData(
    String uri, Map<String, String> body, List<MultipartBody> multipartBody,
    {Map<String, String>? headers}) async {
  await getApiToken();
  if (await ApiChecker.isVpnActive()) {
    return const Response(statusCode: -1, statusText: 'you are using vpn');
  }
  {
    try {
      if (foundation.kDebugMode) {
        logCat('====> API Call: $uri\nToken: $fcmToken');
        logCat('====> API Body: $body with ${multipartBody.length} image ');
      }
      http.MultipartRequest request =
          http.MultipartRequest('POST', Uri.parse(appBaseUrl + uri));
      request.headers.addAll(headers ?? _mainHeaders);
      for (MultipartBody multipart in multipartBody) {
        if (foundation.kIsWeb) {
          Uint8List list = await multipart.file!.readAsBytes();
          http.MultipartFile part = http.MultipartFile(
            multipart.key,
            multipart.file!.readAsBytes().asStream(),
            list.length,
            filename: basename(multipart.file!.path),
            contentType: MediaType('image', 'jpg'),
          );
          request.files.add(part);
        } else {
          File file = File(multipart.file!.path);
          request.files.add(http.MultipartFile(
            multipart.key,
            file.readAsBytes().asStream(),
            file.lengthSync(),
            filename: file.path.split('/').last,
          ));
        }
      }
      request.fields.addAll(body);
      http.Response response0 =
          await http.Response.fromStream(await request.send());
      Response response = handleResponse(response0, uri);
      if (foundation.kDebugMode) {
        logCat(
            '====> API Response: [${response.statusCode}] $uri\n${response.body}');
      }
      return response;
    } catch (e) {
      return Response(statusCode: 1, statusText: noInternetMessage);
    }
  }
}