uploadFile static method

  1. @Deprecated('Please use uploadFiles instead of uploadFile')
Future uploadFile(
  1. String endPoint,
  2. String filename, {
  3. String fileKey = 'image',
  4. bool isUserAvatar = false,
  5. bool hasAuth = true,
  6. Map<String, String>? params,
  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

@Deprecated('Please use uploadFiles instead of uploadFile')
static Future<dynamic> uploadFile(
  String endPoint,
  String filename, {
  String fileKey = 'image',
  bool isUserAvatar = false,
  bool hasAuth = true,
  Map<String, String>? params,
  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);
    var request = MultipartRequest(
      requestType,
      url,
    );
    request.files.add(await MultipartFile.fromPath(fileKey, filename));
    request.headers.addAll(customHeader ?? httpHeader ?? header);
    if (params != null) {
      request.fields.addAll(params);
    }

    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;
}