cancelDownload method

Future<void> cancelDownload({
  1. bool deletePartialDownload = false,
})

Cancels the currently active download.

If deletePartialDownload is true, this method will also attempt to permanently delete the incomplete file from the device's storage.

Implementation

Future<void> cancelDownload({bool deletePartialDownload = false}) async {
  String? pathToDelete = _downloadPath;

  _cancelToken?.cancel();

  if (deletePartialDownload && pathToDelete != null) {
    _downloadPath = null;
    File file = File(pathToDelete);
    if (await file.exists()) {
      try {
        await file.delete();
      } catch (e) {
        log("Error deleting partial file during cancellation", error: e);
      }
    }
  }
}