Flutter WidgetsFlutterBinding.ensureInitialized() is very slow on some Android devices

Viewed 90

I'm building an application that using Firebase as a database. Somehow the starting time of my app is extremely slow on some devices (20% of the Android test devices, most of this 20% are XiaoMi and RealMi) but super fast on others. This makes the app appear as a white/black screen for 15 seconds. It appears that the WidgetsFlutterBinding.ensureInitialized(); take very long, but I can't remove it because it is required for Firebase initialization.

Here is the code I for my main function (with debugging)

Future<void> main() async {
  stopwatch.start();
  print("stopwatch start ${stopwatch.elapsed}");

  WidgetsFlutterBinding.ensureInitialized();
  print("WidgetsFlutterBinding ${stopwatch.elapsed}");

  await Firebase.initializeApp();
  print("Firebase.initializeApp ${stopwatch.elapsed}");

  AppSharedPreference.init();
  print("AppSharedPreference.init(); ${stopwatch.elapsed}");

  EquatableConfig.stringify = !kDebugMode;
  Bloc.observer = SimpleBlocObserver();
  print("Bloc.observer = SimpleBlocObserver() ${stopwatch.elapsed}");

  if (defaultTargetPlatform == TargetPlatform.android) {
    InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
    print("InAppPurchaseAndroidPlatformAddition ${stopwatch.elapsed}");
  }

  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
    print("done getting preferred orientation ${stopwatch.elapsed}");
    runApp(globalBlocProviderForInitialize(globalBlocProviderForApp(App())));
  });
}

And here is the time printed.

enter image description here

Anyone has any idea about this?

0 Answers
Related