uploadFiles static method

Future uploadFiles(
  1. String endPoint,
  2. Map<String, String> fileParams, {
  3. List<File>? files,
  4. bool isUserAvatar = false,
  5. bool hasAuth = true,
  6. Map<String, String>? dataParams,
  7. required String token,
  8. bool? defaultResponse,
  9. Map<String, String>? customHeader,
  10. bool isTypeJson = true,
  11. String? changeLocalization,
  12. String requestType = 'POST',
  13. String tokenKey = 'Bearer ',
  14. bool? useDefaultURl,
  15. bool? showLogs,
  16. bool? usePreCheckFn,
  17. int? callTimeoutInSec,
  18. Future<T> httpCallPreFunction<T>()?,
  19. bool callHttpCallPreFunction = true,
  20. Future<T> httpCallPostFunction<T>()?,
  21. bool callHttpCallPostFunction = true,
})

Implementation

static Future<dynamic> uploadFiles(
  String endPoint,
  Map<String, String> fileParams, {
  List<File>? files,
  bool isUserAvatar = false,
  bool hasAuth = true,
  Map<String, String>? dataParams,
  required String token,
  bool? defaultResponse,
  Map<String, String>? customHeader,
  bool isTypeJson = true,
  String? changeLocalization,
  String requestType = 'POST',
  String tokenKey = 'Bearer ',
  bool? useDefaultURl,
  bool? showLogs,
  bool? usePreCheckFn,
  int? callTimeoutInSec,
      Future<T> Function<T>()? httpCallPreFunction,
      bool callHttpCallPreFunction = true,
      Future<T> Function<T>()? httpCallPostFunction,
      bool callHttpCallPostFunction = true,
}) async {
  Uri url = HttpCalls.getRequestURL(endPoint, useDefaultURl: useDefaultURl);
  dynamic response;
  try {

    if(callHttpCallPreFunction){
      if(httpCallPreFunction != null){
        await httpCallPreFunction();
      }else{
        if(HttpCalls.httpCallPreFunction != null)await HttpCalls.httpCallPreFunction!();
      }
    }
    await callPreCheckFn(usePreCheckFn);
    final Map<String, String> header = {};

    if ((localization ?? changeLocalization) != null) {
      header['X-localization'] = localization ?? changeLocalization ?? '';
      header['Accept-Language'] = localization ?? changeLocalization ?? '';
    }

    if (isTypeJson) {
      header[HttpHeaders.contentTypeHeader] = 'application/json';
    }

    if (hasAuth) {
      header[Static.httpCallTokenKey ?? HttpHeaders.authorizationHeader] =
          '${Static.canHttpCallAddBearerAsPreToken ? tokenKey : ''}$token';
    }
    if (headerAddOns != null) {
      header.addAll(headerAddOns!);
    }




    showLog((customHeader ?? httpHeader ?? header),
        showLog: showLogs, logName: endPoint);

    showLog((dataParams), showLog: showLogs, logName: endPoint);

    showLog((fileParams), showLog: showLogs, logName: endPoint);

    var request = MultipartRequest(
      requestType,
      url,
    );

    if (files != null) {
      for (int i = 0; i < files.length; i++) {
        request.files.add(
          http.MultipartFile(
              'file',
              http.ByteStream(ByteStream(files[i].openRead())),
              await files[i].length(),
              filename: files[i].path),
        );
      }
    } else {
      await Future.forEach(
        fileParams.entries,
        (file) async {
          showLog(file.key, showLog: showLogs, logName: endPoint);
          showLog(file.value, showLog: showLogs, logName: endPoint);
          request.files.add(
            await MultipartFile.fromPath(file.key, file.value),
          );
        },
      );
    }

    request.headers.addAll(customHeader ?? httpHeader ?? header);
    if (dataParams != null) {
      request.fields.addAll(dataParams);
    }

    var streamedResponse = await request.send();
    var result = await Response.fromStream(streamedResponse);
    showLog(result.body.toString(),
        enableJsonEncode: false, showLog: showLogs, logName: endPoint);

    response =
        HttpCalls.getDataObject(result, defaultResponse: defaultResponse);
    if(callHttpCallPostFunction){
      if(httpCallPostFunction != null){
        await httpCallPostFunction();
      }else{
        if(HttpCalls.httpCallPostFunction != null)await HttpCalls.httpCallPostFunction!();
      }
    }
  } catch (e) {
    response = errorHandler(e, response, defaultResponse);
  }

  return response;
}