Startup and Data Loading
Waiting on a network or a disk is not a Dart CPU problem; an isolate will not help. The fix is doing less before the first frame, or doing independent work together.
1. Measure startup first
Profile mode, physical device. flutter run --profile --trace-startup records startup timings, and the DevTools timeline shows what runs before the first frame. Without a measurement, mark the finding "likely, needs measurement".
2. Work before the first frame
Open main() and every initialiser it awaits (storage, auth, remote config, analytics, crash reporting, plugin setup, large asset or font loading).
- Keep only what the first screen truly needs before
runApp.
- Move the rest to after the first frame (for example
WidgetsBinding.instance.addPostFrameCallback) or to the first use.
deferFirstFrame and allowFirstFrame exist, but they delay everything the user sees; use them only when the first frame cannot be drawn without the data, and keep the wait short.
- Independent initialisations can run together with
Future.wait instead of one await after another.
- Do not reorder initialisers that depend on each other, and do not skip crash reporting setup. Preserve behaviour.
3. Requests
| Pattern |
Action |
| Independent calls awaited one after another |
Future.wait (handle each error; one failure should not hide the others unless the screen needs all) |
| Request B needs the result of A |
Leave it sequential; ask whether one backend endpoint could return both (verify it exists; never invent one) |
| Fast navigation or repeated taps, response arrives after the screen moved on |
Ignore stale results: compare a request token or check mounted before applying state; cancel the request if the client supports it |
| Search on every keystroke |
Debounce, and ignore results for an older query |
| Retry on failure |
Retry only safe (idempotent) requests, with a limit and increasing delay; never retry a failing request in a tight loop |
| Whole dataset fetched to show a page |
Check for existing pagination first; see "Client vs backend" in flutter-perf-derived-state |
4. Streams and live feeds
Symptom: many messages per second, each calling setState or updating a provider.
- Update the UI at a bounded rate: keep the latest value and publish it on a timer, or sample/throttle the stream. Keep every message only if the screen needs every message.
- Do not use a
StreamBuilder that shows only the last event where the screen needs a list; accumulate in state.
- Cancel the subscription on dispose (see flutter-perf-memory-and-leaks).
- Keep rebuild scope narrow while updates arrive: flutter-perf-build-and-rebuild.
5. Caches
A cache needs a key, an invalidation rule and a size cap before it is added; the rules are in flutter-perf-derived-state section "Cache safety". Do not add a persistent cache to fix a startup measurement that was not taken.
Verify
Compare time to first frame and time to first useful content before and after, on the same device and, for network work, the same throttled connection (the emulator or a proxy can slow the network). Report measured values only. Method: flutter-perf-profiling-and-validation.
Hard rules
- Act only on what the user asked. Before answering or changing anything, open the relevant files and check what is already done; report only what you checked, never guess, and do not redo finished work. Reason: guesses and repeated work waste the user's credit.
1---2name: flutter-perf-startup-and-data-loading3description: >4 Use when the app is slow to show its first frame, a screen waits on a chain of5 requests, a live feed floods the UI, or search fires a request per keystroke.6 Improves Flutter startup and data-loading time: work done in main() before7 runApp, plugin and service initialisation that can wait, sequential requests8 that could run together, late responses overwriting newer state, unthrottled9 streams or WebSocket messages, and per-keystroke searches. Do not use for10 CPU-bound parsing (flutter-perf-isolates) or backend aggregation decisions11 (flutter-perf-derived-state).12metadata:13 source: https://skillmd.com/skills/kinwisenz-devops/flutter-perf-startup-and-data-loading14---1516# Startup and Data Loading1718Waiting on a network or a disk is not a Dart CPU problem; an isolate will not help. The fix is doing less before the first frame, or doing independent work together.1920## 1. Measure startup first2122Profile mode, physical device. `flutter run --profile --trace-startup` records startup timings, and the DevTools timeline shows what runs before the first frame. Without a measurement, mark the finding "likely, needs measurement".2324## 2. Work before the first frame2526Open `main()` and every initialiser it awaits (storage, auth, remote config, analytics, crash reporting, plugin setup, large asset or font loading).2728- Keep only what the first screen truly needs before `runApp`.29- Move the rest to after the first frame (for example `WidgetsBinding.instance.addPostFrameCallback`) or to the first use.30- `deferFirstFrame` and `allowFirstFrame` exist, but they delay everything the user sees; use them only when the first frame cannot be drawn without the data, and keep the wait short.31- Independent initialisations can run together with `Future.wait` instead of one `await` after another.32- Do not reorder initialisers that depend on each other, and do not skip crash reporting setup. Preserve behaviour.3334## 3. Requests3536| Pattern | Action |37|---|---|38| Independent calls awaited one after another | `Future.wait` (handle each error; one failure should not hide the others unless the screen needs all) |39| Request B needs the result of A | Leave it sequential; ask whether one backend endpoint could return both (verify it exists; never invent one) |40| Fast navigation or repeated taps, response arrives after the screen moved on | Ignore stale results: compare a request token or check `mounted` before applying state; cancel the request if the client supports it |41| Search on every keystroke | Debounce, and ignore results for an older query |42| Retry on failure | Retry only safe (idempotent) requests, with a limit and increasing delay; never retry a failing request in a tight loop |43| Whole dataset fetched to show a page | Check for existing pagination first; see "Client vs backend" in [flutter-perf-derived-state](../flutter-perf-derived-state/SKILL.md) |4445## 4. Streams and live feeds4647Symptom: many messages per second, each calling `setState` or updating a provider.4849- Update the UI at a bounded rate: keep the latest value and publish it on a timer, or sample/throttle the stream. Keep every message only if the screen needs every message.50- Do not use a `StreamBuilder` that shows only the last event where the screen needs a list; accumulate in state.51- Cancel the subscription on dispose (see [flutter-perf-memory-and-leaks](../flutter-perf-memory-and-leaks/SKILL.md)).52- Keep rebuild scope narrow while updates arrive: [flutter-perf-build-and-rebuild](../flutter-perf-build-and-rebuild/SKILL.md).5354## 5. Caches5556A cache needs a key, an invalidation rule and a size cap before it is added; the rules are in [flutter-perf-derived-state](../flutter-perf-derived-state/SKILL.md) section "Cache safety". Do not add a persistent cache to fix a startup measurement that was not taken.5758## Verify5960Compare time to first frame and time to first useful content before and after, on the same device and, for network work, the same throttled connection (the emulator or a proxy can slow the network). Report measured values only. Method: [flutter-perf-profiling-and-validation](../flutter-perf-profiling-and-validation/SKILL.md).6162## Hard rules6364- Act only on what the user asked. Before answering or changing anything, open the relevant files and check what is already done; report only what you checked, never guess, and do not redo finished work. Reason: guesses and repeated work waste the user's credit.