postMethod static method

Future<Response> postMethod({
  1. required Uri url,
  2. Map<String, String>? headers,
  3. Map<String, dynamic>? body,
  4. Map<String, String>? multipartFile,
})

Implementation

static Future<Response> postMethod(
    {required Uri url,
    Map<String, String>? headers,
    Map<String, dynamic>? body,
    Map<String, String>? multipartFile}) async {
  // print(url.path);
  // AppUtils.log(Urls.appApiBaseUrl+url.path);
  AppUtils.log(Urls.api+url.path);
  AppUtils.log(headers);
  AppUtils.log(body);

  // AppUtils.log(multipartFile);
  // AppUtils.log(body);
  if (multipartFile != null) {
    var request = MultipartRequest('POST', url);
    if(headers != null){
      request.headers.addAll(headers);
    }
    for (var entry in multipartFile.entries) {
      String fieldName = entry.key;
      String filePath = entry.value;
      request.files.add(
        await MultipartFile.fromPath(fieldName, filePath),
      );
    }
    if (body != null) {
      for (var entry in body.entries) {
        request.fields[entry.key] = entry.value.toString();
      }
    }
    var streamedResponse = await request.send();
    var response = await Response.fromStream(streamedResponse);
    return response;
  } else {
    return post(url, headers: headers, body: jsonEncode(body));
  }
}