Reputation: 65
i'm building an explorer using flutter getting apps length will take a while so i tried to get the value using annother Isolate, that's part of my main.dart code
Future<int> getValueFromIsolate() async {
return await compute(
apps,
0,
);
}
Future<int> apps(int n) async {
int value = 0;
List apps = await DeviceApps.getInstalledApplications(
includeAppIcons: true,
includeSystemApps: true,
onlyAppsWithLaunchIntent: true,
);
print(apps.length);
value = apps.length;
print(value);
return value;
}
And so there is my main function
void main() {
WidgetsFlutterBinding.ensureInitialized();
getValueFromIsolate().then(
(value) {
print("Then the value is $value");
},
);
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => AppProvider(),
),
ChangeNotifierProvider(
create: (_) => CategoryProvider(),
),
ChangeNotifierProvider(
create: (_) => CoreProvider(),
),
],
child: MyApp(),
),
);
}
But i still got this error
I/flutter ( 6234): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. I/flutter ( 6234): 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 theWidgetsFlutterBinding.ensureInitialized()
first. I/flutter ( 6234): If you're running a test, you can call theTestWidgetsFlutterBinding.ensureInitialized()
as the first line in your test'smain()
method to initialize the binding.
I don't understand what it's happening and i don't know what to do..!! Please need your help Thanks for reading and your help
Upvotes: 1
Views: 1077
Reputation: 1781
Currently Flutter platform channel has limitation - communications are only supported by the main isolate which created when your application launched.
compute()
creates new one for youHowever, there are two plugins to help you out:
flutter_isolate provides a replacement isolate that can communicate to plugins because it creates its own UI backing (nothing that you see or have to deal with, just technically),
isolate_handler The advantage in using this package rather than flutter_isolate itself is that this adds handling capabilities, you can start several isolates, keep track of them and you don't have to set up your own communication between the isolate and the main thread (something you have to do manually with both the original stock Isolate and FlutterIsolate) because it's abstracted away and readily available.
If you still have quesdtion why? or interested to deep dive - read all discussion with Flutter developers in github issue
Upvotes: 3