download function

Future<String?> download({
  1. dynamic fileName,
  2. dynamic url,
  3. dynamic directory,
  4. bool isOpen = false,
  5. bool isShare = false,
  6. dynamic context,
})

Implementation

Future<String?> download({fileName, url, directory, bool isOpen = false, bool isShare = false, context}) async {
  String? id;
  if (await File(directory + '/' + fileName).exists()) {
  } else {
    // _onLoading(context);
    String? data = await FlutterDownloader.enqueue(
      url: url,
      savedDir: directory,
      requiresStorageNotLow: true,
      saveInPublicStorage: true,
      fileName: fileName,
      showNotification: false, // show download progress in status bar (for Android)
      openFileFromNotification: true, // click on notification to open downloaded file (for Android)
    );
    id = data;
  }
  delayed = Timer.periodic(const Duration(seconds: 1), (times) async {
    try {
      if (!isLoad) {
        isLoad = true;
        final bool status = await getDownloadfile(fileName);
        if (status) {
          if (isOpen) {
            OpenFile.open(directory + '/' + fileName);
          } else if (isShare) {
            Share.shareFiles([directory + '/' + fileName]);
          }
          isLoad = false;
          delayed!.cancel();
        } else {
          isLoad = false;
        }
      }
    } catch (_) {
      print(_);
      delayed!.cancel();

      if (isOpen) {
        OpenFile.open(directory + '/' + fileName);
      } else if (isShare) {
        Share.shareFiles([directory + '/' + fileName]);
      }
    }
  });
  return id;
}