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
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
to this :
//
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:
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.