I am using this library(photo_manager) to load images from the device. However loading a lot of images causes Flutter to skip frames so I decided to put the code inside an 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:
[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.
I put this line before runApp 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.
However, there are a couple of plugins that can help with that.
flutter_isolate
isolate_handler
you can get more info here