Search code examples
flutterdartdart-isolatesflutter-downloader

Flutter ReceiverPort don't listen


I'm using the flutter_downloader package to download files with my app. The progress notification is working nicely. but my ReceivePort is not listening to the progress.

  final ReceivePort port = ReceivePort();

     @override
  void initState() {
    super.initState();

     IsolateNameServer.registerPortWithName(
        port.sendPort, 'downloader_sendport');

     port.listen((dynamic data) async {
       log('data: $data');  // don't work

     });
     FlutterDownloader.registerCallback(downloadCallback);
   }


@pragma('vm:entry-point')
  static void downloadCallback(
      String id, DownloadTaskStatus status, int progress) {
    log("downloadCallback => $id, $status, $progress"); // works

    final SendPort? send =
        IsolateNameServer.lookupPortByName('downloader_sendport');

   

    send?.send([id, status, progress]);
  }

Solution

  • The SendPort needs to receive a primitive object (int), not a DownloadTaskStatus object:

    Solution: Change status to primitive status.value when calling send(...):

    send?.send([id, status.value, progress]);