# Flutter Perf Startup And Data Loading

> Use when the app is slow to show its first frame, a screen waits on a chain of requests, a live feed floods the UI, or search fires a request per keystroke. Improves Flutter startup and data-loading time: work done in main() before runApp, plugin and service initialisation that can wait, sequential requests that could run together, late responses overwriting newer state, unthrottled streams or WebSocket messages, and per-keystroke searches. Do not use for CPU-bound parsing (flutter-perf-isolates) or backend aggregation decisions (flutter-perf-derived-state).

- Skill: `kinwisenz-devops/flutter-perf-startup-and-data-loading` (Agent Skill)
- Install (CLI): `npx get-skill add kinwisenz-devops/flutter-perf-startup-and-data-loading`
- Raw SKILL.md: https://api.skillmd.com/api/skills/kinwisenz-devops/flutter-perf-startup-and-data-loading/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: KinwiseNZ-DevOps (https://skillmd.com/u/kinwisenz-devops)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/kinwisenz-devops/flutter-perf-startup-and-data-loading

---


# 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](../flutter-perf-derived-state/SKILL.md) |

## 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](../flutter-perf-memory-and-leaks/SKILL.md)).
- Keep rebuild scope narrow while updates arrive: [flutter-perf-build-and-rebuild](../flutter-perf-build-and-rebuild/SKILL.md).

## 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](../flutter-perf-derived-state/SKILL.md) 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](../flutter-perf-profiling-and-validation/SKILL.md).

## 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.

