downloadFile static method

Future downloadFile(
  1. String url,
  2. String fileName, {
  3. dynamic onProgress(
    1. double percent
    )?,
  4. dynamic onFinished()?,
  5. dynamic onError(
    1. Object e
    )?,
})

Implementation

static Future downloadFile(String url, String fileName,
    {Function(double percent)? onProgress,
    Function()? onFinished,
    Function(Object e)? onError}) async {
  Dio dio = Dio();
// Download the file
  var appDocDir = await appFolder();
  try {
    if (fileName.startsWith("/")) {
      fileName = fileName.substring(1);
    }
    await dio.download(url, "$appDocDir/$fileName",
        onReceiveProgress: (received, total) {
      if (total != -1) {
        var progress = received / total;
        onProgress?.call(progress);

        if (progress == 1) {
          onFinished?.call();
        }
      }
    });
  } catch (e) {
    onError?.call(e);
  }
}