download method

Future download(
  1. String url, {
  2. Map<String, dynamic> query = const {},
  3. CancelToken? cancelToken,
  4. Object? data,
  5. Options? options,
  6. dynamic onSendProgress(
    1. int,
    2. int
    )?,
  7. dynamic onReceiveProgress(
    1. int,
    2. int
    )?,
  8. bool deleteOnError = true,
  9. FileAccessMode fileAccessMode = FileAccessMode.write,
  10. String lengthHeader = Headers.contentLengthHeader,
})

DOWNLOAD: Downloads a file from the specified url and saves it to the user's downloads directory.

Returns the response on success, or handles errors using _handleError.

url: The URL of the file to download.

Implementation

Future<dynamic> download(
  String url, {
  Map<String, dynamic> query = const {},
  CancelToken? cancelToken,
  Object? data,
  Options? options,
  Function(int, int)? onSendProgress,
  Function(int, int)? onReceiveProgress,
  bool deleteOnError = true,
  FileAccessMode fileAccessMode = FileAccessMode.write,
  String lengthHeader = Headers.contentLengthHeader,
}) async {
  try {
    final downloadDirectory = await getDownloadsDirectory();
    final path = "${downloadDirectory?.path}/${url.split('/').last}";

    final response = await _dio.download(
      url,
      path,
      queryParameters: query,
      cancelToken: cancelToken,
      options: options,
      onReceiveProgress: onReceiveProgress,
      data: data,
      deleteOnError: deleteOnError,
      fileAccessMode: fileAccessMode,
      lengthHeader: lengthHeader,
    );
    if (isApiLoggerEnable) {
      await _sqfLiteService.add(data: {
        "url": response.realUri.toString(),
        "request": jsonEncode(response.requestOptions.data),
        "response": jsonEncode(response.data),
        "statusCode": response.statusCode,
        "error": "No Error",
        "header": jsonEncode(response.headers.map),
        "method": response.requestOptions.method,
        "timestamp" : DateTime.now().toIso8601String(),
      });
    }
    return response;
  } catch (e) {
    return _handleError(e);
  }
}