1

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 the WidgetsFlutterBinding.ensureInitialized() first. I/flutter ( 6234): 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.

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

0

2 Answers 2

3

Currently Flutter platform channel has limitation - communications are only supported by the main isolate which created when your application launched.

  1. DeviceApps.getInstalledApplications does invokes platform-channel to access platform API and
  2. you invoke it from isolate - compute() creates new one for you

However, 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

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks again Isolate Handler solve my problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.