skip to Main Content

I’m using flutter_downloader package and I have to get the data in listen method, but it doesn’t work. I don’t receive anything. I read that the issue could be because in send method I have to paste only primitive objects and I’ve changed it, but it still doesn’t work

  final ReceivePort _receivePort = ReceivePort();
  int? status;
  int? progress;
  String? id;

  @override
  void initState() {
    IsolateNameServer.registerPortWithName(_receivePort.sendPort, 'downloader_send_port');
    _receivePort.listen(
      (data) async {
        Logger().e('task listen data: $data'); //doesn't work here
        // setState(() {
        //   id = data[0];
        //   status = data[1];
        //   progress = data[2];
        // });
      },
    );
    FlutterDownloader.registerCallback(downloadCallback);
    super.initState();
  }

  @override
  void dispose() {
    IsolateNameServer.removePortNameMapping('downloader_send_port');
  }

  @pragma('vm:entry-point')
  static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
    final SendPort? send = IsolateNameServer.lookupPortByName('downloader_send_port');
    Logger().e('task send id: $id');
    Logger().e('task send status: ${status.value}');
    Logger().e('task send progress: $progress');
    send?.send([id, status.value, progress]);
  }

in logs I have only data from callback method but nothing from listening

flutter: ^[[38;5;196m│ ⛔ task send id: io.myair.download.task.73570.1683533984.371354<…>
flutter: ^[[38;5;196m│ ⛔ task send status: 1<…>
flutter: ^[[38;5;196m│ ⛔ task send progress: 0<…>
flutter: ^[[38;5;196m│ ⛔ task send id: io.myair.download.task.73570.1683533984.371354<…>
flutter: ^[[38;5;196m│ ⛔ task send status: 3<…>
flutter: ^[[38;5;196m│ ⛔ task send progress: 100<…>

3

Answers


  1. Chosen as BEST ANSWER

    I'm not sure where is the difference, but it works if write it as in example from package https://github.com/fluttercommunity/flutter_downloader/blob/master/example/lib/home_page.dart

    final ReceivePort _receivePort = ReceivePort();
      DownloadTaskStatus? status;
      int? progress;
      String? id;
    
      @pragma('vm:entry-point')
      static void downloadCallback(
        String id,
        DownloadTaskStatus status,
        int progress,
      ) {
        print(
          'Callback on background isolate: '
          'task ($id) is in status ($status) and process ($progress)',
        );
    
        IsolateNameServer.lookupPortByName('downloader_send_port')?.send([id, status.value, progress]);
      }
    
      void _unbindBackgroundIsolate() {
        IsolateNameServer.removePortNameMapping('downloader_send_port');
      }
    
      void _bindBackgroundIsolate() {
        final isSuccess = IsolateNameServer.registerPortWithName(
          _receivePort.sendPort,
          'downloader_send_port',
        );
        if (!isSuccess) {
          _unbindBackgroundIsolate();
          _bindBackgroundIsolate();
          return;
        }
        _receivePort.listen((dynamic data) {
          final taskId = (data as List<dynamic>)[0] as String;
          final status = DownloadTaskStatus(data[1] as int);
          final progress = data[2] as int;
    
          print(
            'Callback on UI isolate: '
            'task ($taskId) is in status ($status) and process ($progress)',
          );
    
          setState(() {
            id = taskId;
            this.status = status;
            this.progress = progress;
          });
        });
      }
    
      @override
      void initState() {
        _bindBackgroundIsolate();
        FlutterDownloader.registerCallback(downloadCallback, step: 1);
        super.initState();
      }
    
      @override
      void dispose() {
        _unbindBackgroundIsolate();
        super.dispose();
      }
    

  2.  void initState() 
    

    to this :

    void didChangeDependencies()
    

    //

    final ReceivePort _receivePort = ReceivePort();
      int? status;
      int? progress;
      String? id;
    
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
        IsolateNameServer.registerPortWithName(_receivePort.sendPort, 'downloader_send_port');
        _receivePort.listen(
          (data) async {
            Logger().e('task listen data: $data');
            // setState(() {
            //   id = data[0];
            //   status = data[1];
            //   progress = data[2];
            // });
          },
        );
        FlutterDownloader.registerCallback(downloadCallback);
      }
    
      @override
      void dispose() {
        IsolateNameServer.removePortNameMapping('downloader_send_port');
      }
    
      @pragma('vm:entry-point')
      static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
        final SendPort? send = IsolateNameServer.lookupPortByName('downloader_send_port');
        Logger().e('task send id: $id');
        Logger().e('task send status: ${status.value}');
        Logger().e('task send progress: $progress');
        send?.send([id, status.value, progress]);
      }
    
    Login or Signup to reply.
  3. It seems like your downloadCallback method is working properly as you are able to see the log message task send id: $id, task send status: ${status.value} and task send progress: $progress in the console.

    However, the listen method on _receivePort is not receiving any data. This might be because the data being sent is not of the expected type or there could be some other issue.

    To further debug the issue, you can try adding a catch block in the _receivePort.listen method and logging any errors that occur:

        _receivePort.listen(
      (data) async {
        try {
          Logger().e('task listen data: $data');
          // ...
        } catch (e) {
          Logger().e('Error in task listen: $e');
        }
      },
    );
    

    This will give you more information about any exceptions that might be occurring in the listen method.

    Additionally, you can try passing a simple string or integer value in the send method to see if it is received properly by the listen method. If it works, then you can check if the data being passed in the send method is of the correct type.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search