Sub topic 9
What Happens Between Tapping the Icon and the First Frame
This chapter assumes basic Flutter and Dart knowledge, including runApp, widgets, asynchronous code, and the difference between debug, profile, and release modes. It also assumes that you can run Flutter commands from a terminal.
The time before the first Flutter frame is the startup path. Work on this path delays the first screen, so startup performance depends on both platform work and Dart work.
The startup phases
When a user taps the app icon, several stages occur:
- The operating system starts the process. Android or iOS creates the app process and displays the configured native launch screen.
- The Flutter engine starts. The engine loads native libraries, creates the rendering surface, and prepares the Dart runtime.
- The Dart isolate starts. An isolate is an independent Dart execution context. Flutter starts the main isolate and calls the Dart
main()function. - Your application initializes. This can include reading preferences, opening a database, loading configuration, registering services, or creating dependency-injection objects.
- Flutter builds the widget tree.
runAppattaches the root widget. Flutter callsbuildmethods to create the first widget tree. - Flutter lays out and paints. The framework calculates sizes, draws the widgets, and submits the result to the engine.
- The first frame appears. The operating system replaces the native launch screen with the first rendered Flutter frame.
The exact details vary by platform and Flutter version. The useful boundary is the same: identify what must happen before the first frame and what can happen afterward.
What commonly delays startup
Synchronous work in main() blocks progress. Examples include decoding a large file, parsing a large JSON document, opening several services, or performing expensive computation before calling runApp.
An await also delays the call that follows it. For example:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await loadRemoteConfiguration();
await openDatabase();
runApp(const MyApp());
}This structure makes the first frame wait for both operations. It may be correct if the first screen cannot function without them, but it is wasteful when the screen could appear with loading or default state.
A slow first build can also delay the frame. Large lists built eagerly, expensive calculations inside build, and image decoding during the first screen are common causes. The first frame should create only the UI needed immediately.
Separate required and optional work
For each startup task, ask:
- Does the first screen need this result to render?
- Can the task begin after
runApp? - Can the screen show a placeholder while it loads?
- Is the task CPU-heavy enough to move to another isolate?
Keep essential setup before runApp. Move analytics, remote configuration refreshes, cache cleanup, and preloading for later screens out of the critical path.
The native splash screen covers platform and Flutter startup, but it does not make the app faster. A useful first Flutter screen should appear quickly and communicate progress when later data is still loading.
Exercise
Your app waits for analytics setup and a remote configuration request before calling runApp. Which change should you consider first?
Show answer
Start the app after only the initialization required by the first screen, then begin analytics and the remote configuration request after the first frame or from the first screen's loading state.