0

in this code I made flutter downloader i want to resume after stop and retry after failed but i get this errror
not found partial downloaded data, this task cannot be resumed or ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No element

   // this is initstate
   void initState() {
   super.initState();
   _bindBackgroundIsolate();

   FlutterDownloader.registerCallback(downloadCallback);
   _isLoading = true;
   _permissionReady = false;
   _prepare();
  }

  //this is where i get the progress and state
  void _bindBackgroundIsolate() {
  bool isSuccess = IsolateNameServer.registerPortWithName(
    _receivePort.sendPort, "downloading");
  if (!isSuccess) {
  _unbindBackgroundIsolate();
  _bindBackgroundIsolate();
  return;
  }
 _receivePort.listen((dynamic data) {
  if (debug) {
    print('UI Isolate Callback: $data');
  }
  /*
   Update UI with the latest progress
   */
  String? id = data[0];
  // print('the status is ${data[1]}');

  int progress = data[2];
  DownloadTaskStatus? status;
  if (data[1] == 0) {
    status = DownloadTaskStatus.undefined;
  } else if (data[1] == 1) {
    status = DownloadTaskStatus.enqueued;
  } else if (data[1] == 2) {
    status = DownloadTaskStatus.running;
  } else if (data[1] == 3) {
    status = DownloadTaskStatus.complete;
  } else if (data[1] == 4) {
    status = DownloadTaskStatus.failed;
  } else if (data[1] == 5) {
    status = DownloadTaskStatus.canceled;
  } else if (data[1] == 6) {
    status = DownloadTaskStatus.paused;
  }

  // int status =
  if (itemsList.isNotEmpty) {
    final item = itemsList.firstWhere((it) => it.itemID == id);
    setState(() {
      item.status = status;
      _theTaskId = id!;
      item.progress = progress;
    });
    }
    });
    }


      //this is the download callback
   static void downloadCallback(
    String id, DownloadTaskStatus status, int progress) {
   if (debug) {
    print(
      'Background Isolate Callback: task ($id) is in status ($status) and process ($progress)');
    }
    final SendPort send = IsolateNameServer.lookupPortByName("downloading")!;
   send.send([id, status.value, progress]);
   }


               // this is when i want to resume the file
                                     if (myItem.status ==
                                      DownloadTaskStatus.paused) {
                                    String? newTaskId =
                                        await FlutterDownloader.resume(
                                            taskId: myItem.itemID!,
                                            requiresStorageNotLow: true,
                                            timeout: 15000);
                                    myItem.itemID = newTaskId;
                                    _resumeDownload(myItem);
                                  }
     //this retry after failed
                                 if (myItem.status ==
                                      DownloadTaskStatus.failed) {
                                    if (myItem.itemID != null) {
                                      String? newTaskId =
                                          await FlutterDownloader.retry(
                                        taskId: myItem.itemID!,
                                        requiresStorageNotLow: true,
                                        timeout: 15000,
                                      );
                                      myItem.itemID = newTaskId;

                                      _retryDownload(myItem);
                                    }
                                  }

i try to set requiresStorageNotLow: true, timeout: 15000, but it seems to be unrelated

ali ma
  • 5
  • 2

0 Answers0