Sub topic 8
Changes Hot Reload Cannot Apply
This chapter assumes that you can write basic Dart, build simple Flutter widgets, distinguish stateless and stateful widgets, and run an app with flutter run or an IDE.
Hot reload updates Dart code in a running debug app and rebuilds affected widgets. It does not rerun every part of the program. In particular, Flutter keeps existing State objects and does not rerun some initialization code. When an edit affects code that already ran only once, hot reload may appear to ignore the edit.
Initialization code does not run again
A State object's initState method runs once, when Flutter creates that object. Hot reload keeps the object, so it does not call initState again.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: CounterPage()));
}
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
late String message;
@override
void initState() {
super.initState();
message = 'Original message';
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(message)),
);
}
}If you change Original message to Updated message and press r, the existing state still contains the original value. Press R for a hot restart. A hot restart creates the application again, so initState runs with the new value.
Changing the build method is different. Hot reload can apply that change because Flutter calls build again.
Changes to main and top-level initializers
Hot reload does not rerun the main function. Edits inside main therefore need a hot restart.
Global variable initializers and static field initializers also run during startup. Hot reload does not rerun them.
const appTitle = 'First title';
int retryLimit = 3;
class Settings {
static String environment = 'test';
}
void main() {
runApp(
const MaterialApp(
home: Scaffold(
body: Center(child: Text(appTitle)),
),
),
);
}Changing appTitle, retryLimit, Settings.environment, or the contents of main requires a hot restart to initialize those values again. A const value used by a rebuilt widget can sometimes update through recompilation, but do not rely on hot reload for startup behavior. Restart when the change concerns application initialization.
| Edit | Hot reload result | Recommended action |
|---|---|---|
| Text or layout inside `build` | Usually updates | Hot reload |
| Value assigned in `initState` | Existing value remains | Hot restart |
| Code inside `main` | Does not rerun | Hot restart |
| Global or static initializer | Does not rerun | Hot restart |
| Native platform code | Cannot be applied | Full restart or rebuild |
Choosing the correct restart
Use hot reload first for changes to widget-building code. Use hot restart when the new code must recreate the Dart application and its state. Stop the app and run it again when the change involves native code, plugin registration, dependency configuration, assets, or another build step outside the running Dart isolate.
A reliable diagnostic sequence
When a change does not appear:
- Confirm that the edited file belongs to the running application.
- Save the file and check the terminal or IDE for reload errors.
- Hot reload once more.
- If the value came from
initState,main, or a global or static initializer, hot restart. - If the change affects a plugin, platform file, asset declaration, or build configuration, stop and run the app again.
- Check whether another widget or cached data supplies the value you are viewing.
The key question is whether the changed statement executes during a rebuild or only during application creation.
Exercise
You change a string assigned in initState, but the old string remains after pressing r. Which action should you take, and why?
Show answer
Press R for hot restart. Hot reload preserves the existing State object, so initState does not run again. Hot restart recreates the application and runs initState with the new string.