【发布时间】: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