0

We have a Flutter app launched in both stores. The app that we have has a terrible launching/starting time and is taking too much time. People are generally stuck on the black screen (They cannot reach the splash screen view that we showing a video) or waiting several seconds until the splash view.

Before we run the app, MyApp or MainApp, we are instantiating lots of libraries such as FlutterSecureStorage, Firebase, Hive & Hive's DBs.

I am open to any suggestion to encounter the problem that we are having.

// one part of the main.dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  lockOrientation();

  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarBrightness: Brightness.dark,
      systemNavigationBarColor: AppColors.transparent,
    ),
  );

  storage = const FlutterSecureStorage(
    iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
    aOptions: AndroidOptions(encryptedSharedPreferences: true),
  );

  await Firebase.initializeApp();
  await Future.wait(
    [
      setupInjector(),
      setupLocator(),
      Hive.initFlutter(),
    ],
  );
  
  // Initializing some hive services to pass Riverpod's provider
  await initModels();
  final services = await initDbServices();
  final downloadServices = await initDownloadDbServices();

  await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kDebugMode);
  FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
  Isolate.current.addErrorListener(RawReceivePort((pair) async {
    final List<dynamic> errorAndStacktrace = pair;
    await FirebaseCrashlytics.instance.recordError(
      errorAndStacktrace.first,
      errorAndStacktrace.last,
    );
  }).sendPort);

  Platform.isIOS
      ? InAppPurchaseStoreKitPlatform.registerPlatform()
      : !DeviceInfo.isHarmonyOS
          ? InAppPurchaseAndroidPlatform.registerPlatform()
          : null;

  RemoteConfigSettings setting =
      RemoteConfigSettings(minimumFetchInterval: const Duration(hours: 1), fetchTimeout: const Duration(minutes: 1));
  await remoteConfig.setConfigSettings(setting);

  NotificationService.instance.listenPushNotifications?.listen((uri) => deeplinkUri = URI);

  return runZonedGuarded(
    () => ProviderScope(child: runApp(MyApp())),
    FirebaseCrashlytics.instance.recordError,
  );
}
Kaan Taha Köken
  • 933
  • 3
  • 17
  • 37
  • Did you consider measuring how long each of these API calls take to determine which you can and which you can't execute in the `main` without affecting the startup time too much? If it only affects your user and not yourself, consider using Firebase Performance Monitoring or a similar tool to measure the real user performance. – Frank van Puffelen Feb 13 '23 at 15:03

1 Answers1

0

You have a couple problem here, one being the long load time of your code. The other problem is that you are waiting for everything to load before you display your splash screen. Instead of using Flutter to display the splash screen, you should take advantage of the native splash screens that display before the Flutter engine is even loaded. That way your users never see a black screen while your app loads. flutter_native_splash is a package that will generate native splash screens for you, or you can check out Flutter's docs for instructions to add the splash screens manually.

jon
  • 3,202
  • 3
  • 23
  • 35