Flutter performance: finding the bottleneck before guessing at it
How to actually locate a jank problem instead of sprinkling const everywhere and hoping.
The usual reaction to a janky screen is to start adding const, wrapping things in
RepaintBoundary and breaking widgets into smaller widgets. Sometimes it works.
Usually it changes nothing, because the problem was a JSON parse on the main thread.
Measure first. It takes five minutes and saves an afternoon.
Profile in profile mode
Debug mode is slow by design. Numbers from it mean nothing.
flutter run --profile
On a real device. Not the simulator, which has your laptop's CPU and will hide exactly the problem you are chasing.
Read the timeline
Open DevTools, Performance tab, and look at which thread the spikes are on. This single distinction tells you what kind of problem you have:
UI thread spikes. Your Dart code is too slow. Build methods doing work, JSON
parsing, sorting a big list, an expensive computation in build.
Raster thread spikes. Drawing is too slow. Usually opacity, clipping, shadows, or too many layers.
Fixing the wrong one is why "I added const everywhere and nothing changed" happens.
The four causes I actually find
1. Work inside build. build runs a lot. Anything expensive in there, such as sorting,
filtering or formatting dates for 200 rows, runs a lot too.
// runs on every rebuild
final sorted = orders..sort((a, b) => b.date.compareTo(a.date));Move it out. Compute it when the data changes, not when the frame does.
2. Rebuilding too much. One setState at the top of a page rebuilds the page. Push
state down to the smallest widget that needs it. BlocBuilder with buildWhen, or a
Consumer on the leaf, does more for performance than any amount of const.
3. Parsing on the main thread. A 2 MB JSON response parsed in the UI isolate is a
frozen app for a beat. compute() moves it off:
final orders = await compute(parseOrders, response.body);4. Images. Full resolution images in a list is the most common raster problem there
is. cacheWidth and cacheHeight on Image decode at display size instead of source
size, and it is often the single biggest win in the app.
Image.network(url, cacheWidth: 320)Lists
ListView(children: [...]) builds every child immediately, even the 400 offscreen ones.
ListView.builder builds what is visible. This is not an optimisation, it is the
correct widget. I still find the first one in production code regularly.
If items are a fixed height, give it itemExtent. Flutter then knows the scroll geometry
without measuring, and long lists get noticeably smoother.
const, and what it is worth
const widgets skip rebuilds. It is a real improvement and it is free, so use it. It is
just not a fix for a slow screen. It is hygiene, not a strategy. Turn on the lint and
stop thinking about it.
Startup time
Users judge the app before they see a screen. Check what is happening before the first frame: SDK initialisations, a synchronous read from storage, an API call blocking the splash. Most of it can be lazy, and most of it is not.
The habit
Reproduce it on a real device, look at the timeline, find the thread, fix that one thing, measure again. Every performance fix I have been confident about came from a before-and-after number. Every one I was not confident about came from guessing.