Sub topic 9
Choosing Reload, Restart, or Full Restart
Assume you know basic Dart syntax, can build simple Flutter widgets, and can run an app with flutter run or an IDE. The three actions below apply to different kinds of changes:
| Action | Keeps Dart state? | Typical use |
|---|---|---|
| Hot reload | Yes | Change widget layout, text, colors, or build logic |
| Hot restart | No | Re-run Dart startup code and reset application state |
| Full restart | No | Rebuild or reconnect platform and native code |
Use hot reload for UI and build-method edits
Hot reload sends updated Dart code to the running debug application. Flutter rebuilds affected widgets, but it keeps existing State objects. This lets you remain on the same screen with the same entered data, scroll position, and counter value.
For example, start this app:
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> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}Tap the button several times. Then change Count: to Total: inside build and perform a hot reload. The label changes, and _count keeps its value.
Hot reload works because Flutter debug mode uses the Dart virtual machine with just-in-time compilation. The running process can receive new Dart function implementations without starting from the beginning.
Use hot restart when startup code or state must run again
Hot restart stops and starts the Dart application isolate. An isolate is Dart's independent execution context. The widget tree and all Dart state are recreated, so _count returns to zero.
Use hot restart when you need to apply changes such as:
- The initial value assigned in
initState. - Code in
main. - A non-const global variable initializer.
- A static field initializer.
- A problem caused by stale in-memory state.
Changing _count = 0 inside initState will not affect an existing State object after hot reload because initState already ran. Hot restart runs initState again.
Use a full restart for native or build changes
A full restart means stopping the app and running it again, often with:
flutter runThis rebuilds the application from the beginning and reconnects it to the device. Choose it when hot restart cannot load the change, including changes to:
- Android or iOS native source.
- Android manifest or iOS configuration.
- Plugin registration or platform channels.
- Build settings, assets, or dependencies.
- Compilation errors that leave the running session unusable.
A full restart takes longer, but it gives the build system a clean process and a fresh application binary when needed.
Make the choice from the change
Use this decision path:
In a terminal running flutter run, press r for hot reload and R for hot restart. Press q to quit. IDEs usually provide buttons with the same actions.
If a reload appears to do nothing, first check that the file was saved and that the app is running in debug mode. Then ask whether the edited code runs only during startup. Try hot restart for that case. If the change crosses into native or build configuration, stop the app and perform a full restart.
State.reassemble runs during hot reload in debug mode. You can use it for development-only diagnostics, but it should not contain required production initialization.
Exercise
You change a widget's color and see the new color, but a value assigned in initState remains unchanged. Which action should you use, and why?
Show answer
Use hot restart. The color is read during a rebuild, while initState ran only when the existing State object was first created.