download method
Future
download(
- String url, {
- Map<
String, dynamic> query = const {}, - CancelToken? cancelToken,
- Object? data,
- Options? options,
- dynamic onSendProgress()?,
- dynamic onReceiveProgress()?,
- bool deleteOnError = true,
- FileAccessMode fileAccessMode = FileAccessMode.write,
- 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);
}
}