Sub topic 12
Troubleshooting a Reload That Seems to Do Nothing
This chapter assumes that you can read basic Dart, build simple Flutter widgets, and distinguish StatelessWidget from StatefulWidget. It also assumes that you can start an app with flutter run or an IDE.
A reload can appear to do nothing for several different reasons. Flutter may not have received the reload command, the edited code may not run during a rebuild, or the change may require a restart. Start by identifying which part failed.
Follow the reload path
Use this sequence:
When using flutter run, confirm that the terminal still shows the running application. Press r in that terminal for hot reload. Flutter prints a message when the reload completes. If the terminal is no longer attached, pressing r elsewhere has no effect.
In an IDE, use its hot reload button and check the debug console. A successful command does not guarantee a visible change, but it confirms that Flutter accepted the request.
Check whether `build` runs the changed code
Hot reload rebuilds affected widgets. It does not execute every statement in the application again. For example, this text changes after a hot reload because it is created in build:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ReloadPage()));
class ReloadPage extends StatefulWidget {
const ReloadPage({super.key});
@override
State<ReloadPage> createState() => _ReloadPageState();
}
class _ReloadPageState extends State<ReloadPage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Updated label, count: $_count'),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}The _count field belongs to the existing State object, so its value survives hot reload. The Text expression runs again when build runs.
Use debugPrint temporarily to confirm that a method executes:
@override
Widget build(BuildContext context) {
debugPrint('Building ReloadPage');
return const Text('Test');
}If the message does not appear, inspect whether the widget is still mounted in the current route or hidden by a condition.
Know when initialization is the problem
initState runs once for a particular State object. Changing an initial value there does not replace the value in an existing object during hot reload.
@override
void initState() {
super.initState();
_count = 10;
}If _count was already initialized, changing 10 to 20 and pressing r will not reset it. Press R for hot restart. A hot restart recreates the Dart application and all its state.
The same reasoning applies to edits in main, global variable initializers, and static field initializers. Hot reload cannot safely rerun these in every existing execution context.
Use `State.reassemble` for reload-only diagnostics
Flutter calls reassemble on a State during hot reload in debug mode. It is useful for temporary development actions, such as logging:
@override
void reassemble() {
super.reassemble();
debugPrint('Reload received; current count is $_count');
}Do not use reassemble for normal application behavior. It is a development hook and does not run in release builds.
Choose the smallest effective action
| Symptom | Action |
|---|---|
| A label or layout in `build` is unchanged | Save the file and hot reload |
| An initial value in `initState` is unchanged | Hot restart |
| `main` or a global initializer changed | Hot restart |
| The app is disconnected or the reload fails | Stop and run the app again |
| Native configuration or plugin setup changed | Full restart, and possibly rebuild |
A full restart means stopping the application and running it again. Use it when the Dart VM cannot apply the change, especially after native platform edits, dependency changes, or a broken debug connection.
Exercise
You change _count = 0 to _count = 100 inside initState, but the running counter remains at 4 after pressing r. What should you do, and why?
Show answer
Press R for hot restart. The existing State object is preserved by hot reload, so initState does not run again. Hot restart creates a new State object and runs initialization again.