[英]Isolate Unhandled exception: E/flutter call the `WidgetsFlutterBinding.ensureInitialized()`
I am using this library (photo_manager) to load images from the device.我正在使用这个库(photo_manager) 从设备加载图像。 However loading a lot of images causes Flutter to skip frames so I decided to put the code inside an Isolate.但是加载大量图像会导致 Flutter 跳帧,所以我决定将代码放在 Isolate 中。 When I run the following code outside the isolate it runs without problems:当我在隔离之外运行以下代码时,它运行没有问题:
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;
}
But when inside the Isolate I get the following error:但是当在 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.
Note PhotoManager.getAssetPathList
is a static method so its isolate safe.注意PhotoManager.getAssetPathList
是一个静态方法,因此它隔离安全。
I put this line before runApp WidgetsFlutterBinding.ensureInitialized();
我把这一行放在 runApp WidgetsFlutterBinding.ensureInitialized();
之前WidgetsFlutterBinding.ensureInitialized();
but it didn't solve the problem.但它没有解决问题。
Anyone knows what is happening?有谁知道发生了什么?
Well I have been facing the same problem and found out that this seems to be a Flutter limitation and that isolates are completely isolated and you can't get access to plugins in isolates.好吧,我一直面临同样的问题,并发现这似乎是 Flutter 的限制,并且隔离是完全隔离的,您无法访问隔离中的插件。
However, there are a couple of plugins that can help with that.但是,有几个插件可以帮助解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.