Using RunApp Twice in Flutter

Randal L. Schwartz on Dart and Flutter · 2K views · 2022 · 11:44

Open on YouTube

Software (other)tutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Flutter questions about async initialization or switching app configuration, because it shows calling runApp twice in main().

Recommended by 1 people across 1 different months, 2022 to 2024.

What it is

A short Flutter tutorial by Randal L. Schwartz showing that runApp can be called twice inside main(), and explaining when that is useful. The typical use is showing a loading screen first, doing async setup, then launching the real app.

What people said

Recommends rashidotm · 2022-08 · Stack Overflow · on “Make flutterfire_ui show some screens in ltr direction rather than locale rtl direction?”

This solution combined with this method [this video] will be even a better workaround. The idea is to use ltr locale in the first runapp and then rtl locale in the second call to runapp. use it with caution though. To put it in other words, you use the first pass of runapp with 'en' locale to sign in the users. Once they are signed in you use the runapp again with 'ar' locale.

Cites Ozan Taskiran · 2024-01 · Stack Overflow · on “Where is the correct place to start application initialization in flutter?”

Guess it depends on your situation. If the initApp function is a Future you can await it at least inside main (making main async). This wouldn't work inside initState in your 2 solution, you would work here with a FutureBuilder or something like that. You can also run runApp twice where you runApp first a loading widget, do your init stuff and call after the real app. Here is an example: [this video]

Cites Dhafin Rayhan · 2024-04 · Stack Overflow · on “How to have async calls at the creation of an ChangeNotifier in Dart / Flutter?”

You can initialize the result value of _addDefaultFoldersIfAccessible as a "warm-up data" before running your app. To allow smooth transition, you can use runApp twice in your main function so that you can show a widget (perhaps a loading screen) before your main app is running. // main.dart Future<void> main() async { runApp(const LoadingScreen()); await yourInitializingFunction(); runApp(const MyApp()); } Ideally, yourInitializingFunction should handle all the necessary async tasks that are required by the ChangeNotifier so that you don't need to make _addDefaultFoldersIfAccessible async anymore. See also: Using RunApp Twice in Flutter [this video]