簡體   English   中英

隔離未處理的異常:E/flutter 調用`WidgetsFlutterBinding.ensureInitialized()`

[英]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(); 但它沒有解決問題。

有誰知道發生了什么?

好吧,我一直面臨同樣的問題,並發現這似乎是 Flutter 的限制,並且隔離是完全隔離的,您無法訪問隔離中的插件。

但是,有幾個插件可以幫助解決這個問題。

顫振隔離

隔離處理程序

你可以在這里獲得更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM