从 Flutter 中的 Isolate listen 方法调用 UI 方法抛出异常

Calling a UI method from Isolate listen method in Flutter throws exception

我尝试使用 ovprogresshud (https://pub.dev/packages/ovprogresshud) to show download progress. I used flutter_downloader (https://pub.dev/packages/flutter_downloader) 下载。

我尝试通过 Isolate 更新进度,但收到错误消息。 (如果我在我的代码中调用 Progresshud.showWithStatusdirectly,在下载之前说,它有效)

我的代码:

ReceivePort _port = ReceivePort();
...

IsolateNameServer.registerPortWithName(
        _port.sendPort, 'downloader_send_port');
_port.listen(
      (dynamic data) {
        String id = data[0];
        DownloadTaskStatus status = data[1];
        int progress = data[2];
        if (status == DownloadTaskStatus.complete) {
        } else if (status == DownloadTaskStatus.running) {
          Progresshud.showWithStatus("%$progress Downloaded");
        }
      },
      onDone: () {
        checkIfDictionaryUnzipped(DBFilePath);
      },
      onError: (error) {},
    );

我收到的错误:

Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.view.ViewParent android.view.ViewGroup.getParent()' on a null object reference, null)
E/flutter (29114): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (29114): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter (29114): <asynchronous suspension>
E/flutter (29114): #2      Progresshud.showWithStatus (package:ovprogresshud/progresshud.dart:18:27)
E/flutter (29114): <asynchronous suspension>
....

查看重要提示here

Important note: your UI is rendered in the main isolate, while download events come from a background isolate (in other words, codes in callback are run in the background isolate), so you have to handle the communication between two isolates.

检查示例,

ReceivePort _port = ReceivePort();

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

    IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
    _port.listen((dynamic data) {
        String id = data[0];
        DownloadTaskStatus status = data[1];
        int progress = data[2];
        setState((){ });
    });

    FlutterDownloader.registerCallback(downloadCallback);
}

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

static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
    final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
    send.send([id, status, progress]);
}