简体   繁体   English

隔离未处理的异常:E/flutter 调用`WidgetsFlutterBinding.ensureInitialized()`

[英]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.但是,有几个插件可以帮助解决这个问题。

flutter_isolate颤振隔离

isolate_handler隔离处理程序

you can get more info here你可以在这里获得更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter WidgetsFlutterBinding.ensureInitialized() 在处理隔离内的 rootBundle 时出现 - Flutter WidgetsFlutterBinding.ensureInitialized() appears when dealing with rootBundle inside an isolate Flutter Web 上未调用 WidgetsFlutterBinding.ensureInitialized 或 Firebase 初始化应用程序 - WidgetsFlutterBinding.ensureInitialized or Firebase initialize app is not called on Flutter web 在 WidgetsFlutterBinding.ensureInitialized() 中给出异常; 等待 Firebase.initializeApp(); - giving exception in WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); 应该使用 WidgetsFlutterBinding.ensureInitialized(); 两次? - should iuse WidgetsFlutterBinding.ensureInitialized(); two times? WidgetsFlutterBinding.ensureInitialized() 有什么作用? - What Does WidgetsFlutterBinding.ensureInitialized() do? 当应用程序以 WidgetsFlutterBinding.ensureInitialized() 启动时,Flutter 模拟时钟(使用 withClock)的行为不一致 - Flutter mocked clock (using withClock) behave inconsistenly when app starts with WidgetsFlutterBinding.ensureInitialized() Flutter Api 调用未处理异常 - Flutter Api call Unhandled Exception Flutter 更新小部件FlutterBinding - Flutter Update WidgetsFlutterBinding 未处理的异常:FormatException:意外字符(在字符 1 处)E/flutter(6084):<br /> E/颤振(6084):^ - Unhandled Exception: FormatException: Unexpected character (at character 1) E/flutter ( 6084): <br /> E/flutter ( 6084): ^ 在 Flutter 中捕获未处理的异常 - Catching unhandled exception in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM