[英]Flutter WidgetsFlutterBinding.ensureInitialized() appears when dealing with rootBundle inside an isolate
[英]Isolate Unhandled exception: E/flutter call the `WidgetsFlutterBinding.ensureInitialized()`
我正在使用這個庫(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();
之前WidgetsFlutterBinding.ensureInitialized();
但它沒有解決問題。
有誰知道發生了什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.