Perceived load time in a Flutter app and a cold-start disk cache
flutterperformancecachecold-startstale-while-revalidateapi-designdropdown
When an app "takes too long to load", the time is rarely in one place. On the TLDR app (Flutter web and Android against a PHP API) a round of measuring found four separate causes, only one of them a slow query. The fixes are general: fewer round trips before the first paint, paint what you already have, and keep the last answer on disk for the next launch.
Find where the time goes
- Time the server side first (see The missing index behind a slow list endpoint). If the query takes 10 ms and the screen takes a second, the client is the problem.
- Then list the requests a screen makes before it paints and whether they run one after another. The post page waited for the post, then made a second round trip for the viewer's vote, flag and view count before painting, then fired one request per response for response votes, and downloaded the whole post list again to compute "related posts".
- Do the same for app start. The startup code held the first frame for two sequential requests (bookmarks, then settings).
Put the viewer's state in the same response
If a screen always needs "the thing" plus "my relationship to the thing" (my vote, my flag, my votes on each child), return them together: my_vote, my_flag, and a map my_response_votes: {responseId: vote}. One request replaces 2 + N. Keep the old endpoints, and have the client fall back to them when the field is absent, so an old server or a share-link read that has no viewer still works. Fire-and-forget anything the screen does not need to draw (the view-count ping).
Seed the detail page from the list
The list already holds the row that was tapped. Look it up in the list cache (peekPost(id)), paint the title and body at once with skeletons where the responses will go, and swap in the full object when it arrives. Rules:
- A seeded page stays up if the refresh fails (offline), but a real 404 must still replace it with "not found". Have the fetch return (post, gone) so the two cases are distinct.
- Any mutation that changes what lists show (pin, archive, delete) must go through the service that owns the list caches and invalidate them. A pin button that called the API directly from the screen left every list showing the old state until a manual refresh. See Singleton services, session caches, and cross-user data leaks in Flutter.
A cold-start cache on disk
In-memory stale-while-revalidate makes tab switches instant, but the first launch still shows a spinner. Persist the small set of things the first screen needs and restore them before the first frame:
- What: the last first page of the list (with the page, limit, order and total it was fetched with), the full list if the app uses one, the user's settings, and bookmark ids. Settings matter most, because layout and the start route depend on them and the app would otherwise wait for them.
- Where: one small wrapper with two backends, files under the app's cache directory on mobile and desktop and
localStorageon the web. - Envelope:
{v, user, ...}. Loading returns only an entry that belongs to the signed-in user and deletes anything else. Bumpvwhen the shape changes. - Disposable by design: every failure (missing file, bad JSON, wrong version) is a cache miss, never an error.
- Restored data carries no fetch time, so screens paint it and still revalidate in the background.
- Startup order: restore from disk, paint, refresh in the background. Wait on the network only when nothing is saved (first run after sign-in), and then run the requests in parallel.
- A failed refresh keeps the current values. The settings loader used to fall back to all defaults when its request failed, which would undo the restore on a bad connection.
- Throttle rewrites of large entries (at most once a minute for a full list) and serialize writes so two saves cannot interleave.
Invariants for safety: the disk entries are deleted by the same reset that clears the in-memory caches on every session transition, nothing is saved while an admin is impersonating another user, and anything end-to-end encrypted stays out. The trade is that private but unencrypted content now sits in the cache directory or localStorage until sign-out; decide that on purpose.
To test it, load the release web build twice in headless Chrome, the second time with the API host made unresolvable, and check that the list still paints (Headless Chrome testing gotchas).
Short list first, full list second
A picker with 34 entries is slow to load and slow to read. The pattern that replaced it: - Show a top 10: the 5 most recently used plus the 5 busiest of the rest. "Busiest" counts activity in the last 90 days, with the all-time count as a tie-break; ranking by all-time counts alone keeps dead entries pinned forever. Rank per user. - Never hide the rest for good. A "Show all (N more)" row at the bottom keeps new and never-used entries reachable. - Two requests in order: the short list comes from a database-only endpoint (about 0.1 s), the full list (which needed a slower call to another service) loads after it and becomes the authority. A ranked name missing from the full list is dropped; an empty short list shows the full list outright, which also covers new users and an old server. - "Show all" inside a Flutter dropdown is a menu row with a sentinel value. Selecting it expands the list and reopens the menu; pin that behavior with a widget test, because it depends on framework internals. - Check every surface that lists the same things. The picker was fixed and the complaint came straight back, because a grid of cards on another page loaded the same full list.
Gotchas
- A
Flexibleinside aRowwith unbounded width asserts in debug and silently works in release; give aDropdownButtonisExpanded: trueinside a bounded parent. - Building every item of a long page eagerly (a
Columnof rendered markdown inside a scroll view) costs first-frame time; a lazy list fixes that but conflicts with one selection area across the whole page (Rendering markdown in Flutter (selection, code-block builders, tappable task lists)) and with auto-scrolling to the end. - A full list that ships every body (about 900 KB of JSON here) is a payload problem no cache hides on the first run; a list without bodies is the next step.
See also
- Singleton services, session caches, and cross-user data leaks in Flutter
- The missing index behind a slow list endpoint
- Flutter app architecture, state management and packages (2026 survey)