Sub topic 9
Fixing Common Causes of Slow Frames
Assume you can build a Flutter app, read the Performance view, and identify whether a slow frame is busy on the UI thread or raster thread. A slow frame is a frame that misses its time budget. The fix should target the work shown in the trace, not a guess about what might be slow.
Start with the measured cause
First capture a repeatable baseline:
- Run the app in profile mode on the target device.
- Open the Performance view and record the slow interaction.
- Select a slow frame.
- Note whether the UI bar, raster bar, or both exceed the frame budget.
- Change one thing and repeat the same interaction.
Use this decision path:
Reduce UI-thread work
The UI thread handles Dart code, widget building, layout, and much of the framework's coordination. Common causes include expensive loops in build, repeated JSON parsing, and rebuilding a large subtree for a small state change.
Keep build cheap. Move expensive computation out of it, and start that computation once rather than during every rebuild:
import 'dart:isolate';
import 'package:flutter/material.dart';
int slowSum() {
var total = 0;
for (var i = 0; i < 50000000; i++) {
total += i % 7;
}
return total;
}
class FixedText extends StatefulWidget {
const FixedText({super.key});
@override
State<FixedText> createState() => _FixedTextState();
}
class _FixedTextState extends State<FixedText> {
late final Future<int> _result = Isolate.run(slowSum);
@override
Widget build(BuildContext context) {
return FutureBuilder<int>(
future: _result,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
return Text('Result: ${snapshot.data}');
},
);
}
}An isolate is a separate Dart execution context. Isolate.run performs the calculation away from the UI isolate. This helps with CPU-heavy Dart work, but it does not make widget building or painting free. Avoid creating a new future inside build, because every rebuild could restart the computation.
Other UI-thread fixes include:
- Split large widgets into smaller widgets so state changes rebuild less content.
- Use
constconstructors where the widget inputs do not change. - Parse, sort, or filter data before displaying it, and cache results when the inputs are unchanged.
- Use lazy lists such as
ListView.builderfor large collections. - Avoid performing synchronous database or file operations during interaction.
Reduce raster-thread work
The raster thread turns the recorded scene into pixels. Large images, many translucent layers, shadows, clips, and complex custom painting can make rasterization expensive.
Try these targeted changes:
- Decode images near their display size instead of loading very large files.
- Avoid unnecessary
Opacity, overlapping transparency, and wide blur effects. - Keep clips and shadows around small regions.
- Simplify
CustomPainterpaths and avoid repainting unchanged content. - Use
RepaintBoundarywhen an independent subtree changes separately from expensive neighboring content. Do not add it everywhere, because each boundary can increase memory and compositing work.
| Trace symptom | Likely fix |
|---|---|
| UI bar is high | Reduce Dart, build, layout, or state-update work |
| Raster bar is high | Reduce painting, images, effects, or layers |
| Spike happens only on first use | Investigate shader compilation or resource upload |
Shaders and first-use jank
A shader is a small graphics program used by the GPU for effects such as gradients and shadows. A new effect can cause a first-use spike while the graphics system prepares it. Check whether later frames are normal. Test on the target devices and Flutter rendering configuration. Impeller can reduce some shader-compilation problems, but it does not remove all painting or GPU costs.
Exercise
Exercise
A selected frame has a high UI bar, while the raster bar stays below budget. A JSON parser runs inside build(). What change should you try first, and how will you verify it?
Show answer
Move parsing out of build(), cache or compute the result once, then repeat the same interaction in profile mode and compare the selected frame's UI time with the baseline.