downloadUri method Null safety

  1. @override
Future<Response> downloadUri(
  1. Uri uri,
  2. dynamic savePath,
  3. {ProgressCallback? onReceiveProgress,
  4. CancelToken? cancelToken,
  5. bool deleteOnError = true,
  6. String lengthHeader = Headers.contentLengthHeader,
  7. dynamic data,
  8. Options? options}
)
override

Download the file and save it in local. The default http method is 'GET', you can custom it by Options.method.

uri: The file url.

savePath: The path to save the downloading file later. it can be a String or a callback:

  1. A path with String type, eg 'xs.jpg'
  2. A callback String Function(HttpHeaders responseHeaders); for example:
  await dio.downloadUri(uri,(HttpHeaders responseHeaders){
     ...
     return '...';
   });

onReceiveProgress: The callback to listen downloading progress. please refer to ProgressCallback.

lengthHeader : The real size of original file (not compressed). When file is compressed:

  1. If this value is 'content-length', the total argument of onProgress will be -1
  2. If this value is not 'content-length', maybe a custom header indicates the original file size , the total argument of onProgress will be this header value.

you can also disable the compression by specifying the 'accept-encoding' header value as '*' to assure the value of total argument of onProgress is not -1. for example:

await dio.downloadUri(uri, './example/flutter.svg',
options: Options(headers: {HttpHeaders.acceptEncodingHeader: '*'}),  // disable gzip
onProgress: (received, total) {
  if (total != -1) {
   print((received / total * 100).toStringAsFixed(0) + '%');
  }
});

Implementation

@override
Future<Response> downloadUri(
  Uri uri,
  savePath, {
  ProgressCallback? onReceiveProgress,
  CancelToken? cancelToken,
  bool deleteOnError = true,
  String lengthHeader = Headers.contentLengthHeader,
  data,
  Options? options,
}) {
  return download(
    uri.toString(),
    savePath,
    onReceiveProgress: onReceiveProgress,
    lengthHeader: lengthHeader,
    deleteOnError: deleteOnError,
    cancelToken: cancelToken,
    data: data,
    options: options,
  );
}