Sub topic 3
Choosing Reload, Restart, or Full Restart
This chapter assumes you know the difference between StatelessWidget and StatefulWidget, can run an app with flutter run, and have read how hot reload and hot restart differ in principle. Here you decide which action to use for each edit.
Choosing Reload, Restart, or Full Restart
Use hot reload first. It keeps your State objects and is the fastest option. Use hot restart when the change touches startup code or state initialization. Use a full restart when the app's build process itself must run again.
| Change | Action | Keeps current state? |
|---|---|---|
| Edit a `build` method | Hot reload | Yes |
| Edit `initState` | Hot restart | No |
| Edit `main()` | Hot restart | No |
| Edit a non-`const` global or static initializer | Hot restart | No |
| Add a new package or change native code | Full restart | No |
When Only a Full Restart Works
A full restart stops the app and runs flutter run again. Use it when you add a package, change pubspec.yaml, edit Android or iOS native code, or change assets that the app loads at build time. Hot restart cannot load new native plugins, so these changes need a full restart.
Running Code on Reload with State.reassemble
State.reassemble is a method that Flutter calls on each State during a hot reload. Use it to refresh cached data or re-create objects that depend on changed code. It runs only in debug mode.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ReassemblePage()));
class ReassemblePage extends StatefulWidget {
const ReassemblePage({super.key});
@override
State<ReassemblePage> createState() => _ReassemblePageState();
}
class _ReassemblePageState extends State<ReassemblePage> {
int _count = 0;
@override
void reassemble() {
super.reassemble();
debugPrint('Hot reload happened, _count is still $_count');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}Troubleshooting a Reload That Seems to Do Nothing
Check these causes in order:
- Read the terminal. A compile error means the reload failed, so fix the error and save again.
- Confirm that you edited the code in
initState,main, or a global initializer. If so, run a hot restart withR. - Confirm that the changed widget is on screen. A page that the user has not opened yet does not rebuild visibly.
- If nothing else works, stop the app and run
flutter runagain.
Platform and Build-Mode Caveats
Hot reload and hot restart work only in debug mode. Release builds, started with flutter run --release, do not support them. Profile mode also does not support them. Behavior can differ slightly across platforms, so test native changes with a full restart.
Exercise
You changed the initial value of a static field and pressed r. The app still shows the old value. What should you do, and why?
Show answer
Press R to run a hot restart. Hot reload does not rerun static or global initializers, so only a restart applies the new value.