download method
Downloads a file.
The query parameters are extracted from request
's toJson
Eventual additional data is extracter from request
's toData
Optionally you can specify options
to pass to Dio
cancelOnDispose
lets you cancel the request if this service is disposed
expectedStatusCode
to check the result of the request
set allowCache
to true
to skip the expectedStatusCode
check when the response
is a cached one (HTTP code 304)
onReceiveProgress
allows to know about the status of the download
deleteOnError
deletes the file if an error occurs
Implementation
@protected
Future<void> download({
required RequestBase request,
required String path,
Options? options,
bool cancelOnDispose = true,
int expectedStatusCode = 200,
bool allowCache = true,
bool deleteOnError = true,
void Function(int count, int total)? onReceiveProgress,
}) async {
try {
final response = await dioInstance.download(
request.endpoint,
path,
queryParameters: request.toJson(),
options: options,
cancelToken: cancelOnDispose ? getNextToken() : null,
deleteOnError: deleteOnError,
onReceiveProgress: onReceiveProgress,
data: request.toData(),
);
if (!(allowCache && response.statusCode == 304)) {
_assertStatusCode(expectedStatusCode, response.statusCode ?? -1);
}
} on DioError catch (error) {
if (error.type == DioErrorType.cancel) {
throw RequestCanceledException(error);
}
throw ApiException.fromDioError(error);
} on HttpServiceException catch (_) {
rethrow;
} catch (e, s) {
throw DownloadException(e.toString(), s);
}
}