Sub topic 12
A repeatable workflow from symptom to verified fix
This chapter assumes you have used the earlier tools in this tutorial: the Widget Inspector, Layout Explorer, Logging view, debugger, Performance view, CPU Profiler, Memory view, Network view, and App Size tool. It also assumes you know the difference between debug, profile, and release mode. Here you will tie them together into a process you can repeat on any bug.
Why a workflow matters
Most wasted debugging time comes from opening the wrong tool, or from changing code before you know what is wrong. A fix that "feels faster" is not a fix until you have measured it. A workflow gives you four steps that stop both mistakes:
- Describe the symptom precisely.
- Pick the tool that matches the symptom and set up the right run mode.
- Form one hypothesis, change one thing, and record the evidence.
- Verify the fix with the same measurement you used to find the problem.
The rest of the chapter covers each step.
Step 1: Describe the symptom precisely
"The app is slow" is not a symptom you can act on. A useful symptom names what the user sees, where, and when. Compare these:
- Vague: "The list is bad."
- Precise: "Scrolling the product list on a mid-range Android phone drops frames when images first appear."
A precise description tells you the screen, the action, the device, and the trigger. Write it down. You will compare against it at the end.
Symptoms fall into a few families. Each family points to a first tool.
| Symptom | Likely cause | First tool | Run mode |
|---|---|---|---|
| Yellow and black overflow stripes, or a widget in the wrong place | Constraints do not match what the widget wants | Widget Inspector and Layout Explorer | Debug |
| Wrong value on screen, or code takes an unexpected branch | Logic error | Logging view, then debugger | Debug |
| Dropped frames, stutter while scrolling or animating | Expensive build, layout, or paint work | Performance view | Profile |
| Frames are slow but the cause is not visible in the frame chart | Expensive Dart code | CPU Profiler | Profile |
| Memory climbs and never falls, or the app is killed in the background | Leaked objects or oversized data | Memory view | Profile |
| Data missing, stale, or slow to arrive | Failed, slow, or repeated requests | Network view | Debug or profile |
| Download or install size is too large | Large assets or dependencies | App Size tool | Release build analysis |
Use the table as a starting point. Symptoms can overlap, and the first tool may point you to a second one. A slow screen might turn out to be a slow network call, which you only find after the Performance view shows the UI thread is idle.
Step 2: Choose the run mode before you measure
Run mode changes the numbers, so choose it before you collect any evidence.
- Debug mode adds assertions, a slower runtime, and hot reload. Use it for layout, logic, and logging. Never judge speed from it.
- Profile mode is close to release speed and still lets DevTools connect. Use it for anything about performance or memory.
- Release mode removes debugging support entirely. DevTools cannot attach to it. You only use it to produce builds for size analysis or final checks.
Start the app in profile mode on a real device, then start DevTools separately:
# Run the app in profile mode on a connected device
flutter run --profile
# Start DevTools on its own, then connect using the VM service URI printed by flutter run
dart devtoolsAn emulator or simulator is fine for layout and logic bugs. For jank and memory, a real device matters, because desktop-class CPUs hide problems that a phone shows.
Step 3: Measure a baseline
Before you change anything, capture a baseline using the tool you picked. The baseline is what makes step 4 meaningful. What you record depends on the tool:
- Layout: a screenshot of the error and the constraint values from Layout Explorer.
- Logic: the log lines or the variable values at the pause point.
- Jank: the count of janky frames, and the longest frame time, over a fixed action such as scrolling the list from top to bottom twice.
- Memory: the instance count of a suspect class after opening and closing a screen five times.
- Network: the number of requests and total time for one user action.
Keep the action the same each time. If you scroll fast for the baseline and slowly for the check, the comparison is meaningless.
Step 4: Form one hypothesis and change one thing
Look at the evidence and write a sentence of the form "I think X causes Y because Z." For example: "I think the stutter is caused by decoding large images on the UI thread, because the frame chart shows long raster time only when new images appear."
Then make one change. If you change three things and the problem goes away, you do not know which change mattered, and you may have added two unneeded complications.
Worked example: layout
The symptom is a RenderFlex overflow error on a narrow screen. The Layout Explorer shows the Text inside the Row asking for more width than remains. The hypothesis is that a Row gives its children unbounded width along its main axis, so the long text has no limit. The one change is to wrap the text in Expanded:
import 'package:flutter/material.dart';
class OverflowDemo extends StatelessWidget {
const OverflowDemo({super.key});
@override
Widget build(BuildContext context) {
return const Row(
children: [
Icon(Icons.info),
// Without Expanded, this long text overflows the Row and Flutter reports a RenderFlex overflow.
Expanded(
child: Text(
'A very long message that does not fit on one line of a narrow screen.',
),
),
],
);
}
}Verification here is simple: the stripes are gone and the error no longer appears in the Logging view. Also test a second, narrower width, because a fix that works on one size can fail on another.
Worked example: logic
The symptom is a negative order total. A log line shows the discount is larger than the total, so you add a conditional pause that fires only in the bad case:
import 'dart:developer';
double applyDiscount(double total, double discount) {
final result = total - discount;
// Pause the app in the debugger only when the unexpected case happens.
debugger(when: result < 0, message: 'Negative total');
return result;
}When the pause triggers, inspect the call stack. It tells you which caller passed the bad discount. The fix belongs there, not in applyDiscount. Remove the debugger call after the fix, since it is a debugging aid and not application logic.
Worked example: performance
A named timeline event helps you see your own code as a labeled block in the Performance view:
import 'dart:developer';
int sumOfSquares(List<int> values) {
return Timeline.timeSync('sumOfSquares', () {
var total = 0;
for (final v in values) {
total += v * v;
}
return total;
});
}If the block is wide inside a janky frame, the hypothesis is confirmed: this call is too expensive for the UI thread. Candidate changes include moving the work off the UI thread with Isolate.run, caching the result, or reducing the input size. Pick one.
Worked example: memory
The symptom is memory that grows each time a user opens and closes a screen. In the Memory view, the instance count of a class from that screen keeps rising after each visit. Look at what that screen creates and owns. A common culprit is a controller that is never released:
import 'package:flutter/material.dart';
class Pulse extends StatefulWidget {
const Pulse({super.key});
@override
State<Pulse> createState() => _PulseState();
}
class _PulseState extends State<Pulse> with SingleTickerProviderStateMixin {
late final AnimationController _controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1))
..repeat();
@override
void dispose() {
// Omitting this line leaves the controller and its ticker alive after the widget is removed.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(opacity: _controller, child: const FlutterLogo());
}
}The one change is adding _controller.dispose(). Streams, listeners, timers, and focus nodes follow the same rule: whatever you create in a State object, release in dispose.
Step 5: Verify with the same measurement
Repeat the baseline procedure exactly: same device, same run mode, same action, same tool. Then compare numbers, not impressions.
- Layout: the error is gone at several widths.
- Logic: the bad branch no longer triggers, and the log shows expected values.
- Jank: the janky frame count and longest frame time dropped over the same scroll.
- Memory: after five open-and-close cycles and a manual garbage collection in the Memory view, the instance count returns to near its starting value.
- Network: fewer requests, or less total time, for the same action.
For timing results, run the measurement two or three times. Frame times vary from run to run, so a single better result may be noise. Look for a consistent improvement.
If the numbers did not improve, you did not fix the problem. Revert the change if it added complexity, return to the hypothesis step, and use the new evidence. A failed attempt still taught you something about where the problem is not.
Check for regressions
A fix can move the problem instead of removing it. Moving work off the UI thread can raise memory use. Caching images can reduce network traffic and increase memory. After verifying the target metric, glance at the other views once: frame chart, memory graph, and network list.
Common mistakes
- Measuring in debug mode and calling the result final.
- Changing several things at once, so the cause stays unknown.
- Comparing different actions between the baseline and the check.
- Leaving
debugger()calls or noisy logs in the code after the fix. - Treating one good run as proof, when frame times naturally vary.
- Optimizing the part you can see in the code instead of the part the profiler shows is slow.
Putting it together
The tools in this tutorial answer different questions. The workflow decides which question to ask first and how to know you have answered it. The core habits are to define the symptom, match the tool and mode, measure before and after, and change one thing at a time. Those habits carry over to problems this tutorial did not cover.
Exercise
Users report that a chat screen "gets slower the longer the app is open." You open the Memory view in profile mode and see that the instance count of MessageTile states grows by roughly 20 each time you leave and re-enter the screen. Using the workflow in this chapter, write the hypothesis, the one change you would make, and how you would verify it.
Show answer
Hypothesis: something created in the chat screen's State (a controller, stream subscription, listener, or timer) is not released in dispose, so the old State objects and their tiles stay reachable. Change: find the resource the screen creates and release it in dispose (for example cancel the stream subscription or dispose the controller), changing only that. Verify: in profile mode on the same device, repeat the same action of leaving and re-entering the screen five times, force a garbage collection in the Memory view, and confirm the MessageTile instance count returns to near its starting value instead of growing by about 20 per visit. Repeat once to rule out noise, then check frame times to confirm no regression.
Exercise
Your teammate says they fixed scroll jank, and shows that the janky frame count fell from 30 to 5 while running with flutter run in debug mode. Give two reasons this does not verify the fix.
Show answer
First, debug mode is not representative of real performance, so the numbers in it do not show how the app will behave for users. The measurement must be repeated in profile mode, ideally on a real device. Second, a single comparison may not be reliable, and it only counts if the baseline was measured under the same conditions (same device, same mode, same scroll action). A baseline taken in a different mode or with a different action cannot be compared to the new number. Repeating the measurement a few times also rules out run-to-run noise.