【问题标题】:Isolate Unhandled exception: E/flutter call the `WidgetsFlutterBinding.ensureInitialized()`隔离未处理的异常:E/flutter 调用 `WidgetsFlutterBinding.ensureInitialized()`
【发布时间】:2021-01-18 22:05:46
【问题描述】:

我正在使用这个library(photo_manager) 从设备加载图像。但是加载大量图像会导致 Flutter 跳帧,所以我决定将代码放在 Isolate 中。当我在隔离之外运行以下代码时,它运行没有问题:

Future<List<AssetEntity>> _getImagesInDeviceAsAssetEntities({int start, int end}) async {
    List<AssetPathEntity> list = await PhotoManager.getAssetPathList(
        type: RequestType.image, onlyAll: true);
    final AssetPathEntity pathEntity = list[0];
    final List<AssetEntity> assetEntityList = await pathEntity.getAssetListRange(start: start, end: end);
    return assetEntityList;
  }

但是当在 Isolate 内部时,我收到以下错误:

[ERROR:flutter/runtime/dart_isolate.cc(865)] Unhandled exception:
E/flutter ( 2494): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 2494): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 2494): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.

注意PhotoManager.getAssetPathList 是一个静态方法,因此它的隔离是安全的。

我把这行放在runApp WidgetsFlutterBinding.ensureInitialized();之前,但没有解决问题。

有人知道发生了什么吗?

【问题讨论】:

    标签: flutter dart dart-isolates


    【解决方案1】:

    好吧,我也遇到了同样的问题,发现这似乎是 Flutter 的限制,并且隔离区是完全隔离的,您无法访问隔离区中的插件。

    但是,有几个插件可以帮助解决这个问题。

    flutter_isolate

    isolate_handler

    您可以获取更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2019-07-02
      • 2021-01-01
      • 2023-04-03
      • 2023-03-13
      • 2018-11-06
      • 1970-01-01
      相关资源
      最近更新 更多