Sub topic 6
Hot Restart: Resetting State Quickly
This chapter assumes you can write basic Dart, build simple Flutter widgets, tell StatelessWidget from StatefulWidget, and run an app with flutter run or from an IDE. It also assumes you have used hot reload, which keeps state, and you now want its counterpart.
What Hot Restart Does
Hot restart applies your code changes and also throws away the app's state. The app returns to its starting point, as if you had launched it fresh, but without the cost of a full rebuild and reinstall.
Hot reload patches new code into the running Dart VM and rebuilds the widget tree. It leaves existing State objects alone. Hot restart does something different:
- It recompiles the changed Dart code.
- It loads the new code into the Dart VM, the same debug-mode runtime that hot reload uses.
- It discards the running Dart state: every
Stateobject, global variable, and static field. - It runs
main()again, so the app builds a new widget tree from scratch.
The process on the device, the VM, and the connection to your tools all stay alive. Only your Dart program restarts. That is why hot restart is usually slower than hot reload but much faster than stopping the app and running it again.
Why Hot Restart Resets State
Hot reload keeps state because it keeps the objects that hold it. Flutter's element tree stays in place, each element keeps its State object, and the framework calls build again on those objects.
Hot restart gives up that continuity. The VM discards the old isolate's heap and starts the program over, so the objects are gone. The new main() calls runApp, and Flutter creates new elements and new State objects. initState runs again for every stateful widget, and each field gets its initializer again.
State that you might expect to survive, but that does not, includes:
- Fields in
Stateclasses, such as a counter or a text field's content. - Scroll positions held in a
ScrollControllerthat you created in aState. - The current route and navigation stack, which are back to
homeorinitialRoute. - Global variables and static fields, which return to their initial values.
- In-memory caches, singletons, and open streams that live in Dart objects.
State that lives outside the Dart program is not touched. Data in a database file, shared preferences on disk, or a server's records is still there after a restart. If your app reads that data in initState or main, it reads it again.
Seeing the Difference
Use the counter from the earlier chapters. Run it, tap the button until the count is 5, then compare the two actions.
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),
),
);
}
}- Change
'Count: $_count'to'Total: $_count'and pressr. The label changes and the number stays at 5. - Change it back to
'Count: $_count'and pressR. The label changes and the number returns to 0.
The edit is identical in both cases. Only the action differs, and the action decides whether the old State object survives.
Where Hot Restart Is the Right Tool
Hot restart is the fix for a specific group of edits that hot reload cannot apply. These are the edits where the code that matters already ran and will not run again on a reload.
Edits to initState
initState runs once, when a State object is created. A hot reload keeps the existing State, so your edit never executes for that object.
class _CounterPageState extends State<CounterPage> {
late int _count;
@override
void initState() {
super.initState();
// Changing this value and hot reloading does not update an existing State,
// because initState already ran. A hot restart runs it again.
_count = 0;
}
@override
Widget build(BuildContext context) {
return Text('Count: $_count');
}
}If you change _count = 0 to _count = 10 and press r, the screen still shows the old value. Press R and the new State object runs the changed initState, so the screen shows 10.
Edits to main
main() runs once when the program starts. Hot reload does not call it again, so changes to main, including the widget you pass to runApp, are ignored. A hot restart calls the new main().
Global and static initializers
Global variables and static fields are initialized once. Hot reload does not re-run their initializers, so an edited initial value does not take effect.
// Global variable and static field initializers are not rerun by hot reload.
const greeting = 'Hello';
int globalCounter = 10;
class Config {
static int retries = 3;
}
void main() {
// Edits to main() are also not applied by hot reload.
runApp(const MaterialApp(home: Scaffold(body: Text(greeting))));
}If you change retries = 3 to retries = 5, a hot reload leaves the field at 3. A hot restart reinitializes it to 5.
Hot Restart Is Also a State Reset
You do not need a code change to use hot restart. It is a quick way to get back to a known starting point. Typical reasons:
- You are testing a first-launch flow and want the app back at its initial screen.
- A bug left a widget in a bad state, and you want to check that the bug is in the state and not in the code.
- You added a new field to a
Stateclass and the old object does not have it yet. Some changes to the shape of a class are safer after a restart. - You want to confirm that your app starts correctly from nothing, which hot reload cannot show you.
That last point matters. An app can look correct after many hot reloads because it carries state that a real launch would never produce. A hot restart before you commit is a cheap check.
Triggering a Hot Restart
Use the terminal where flutter run is active:
flutter run
# In the terminal where flutter run is active:
# r -> hot reload
# R -> hot restart
# q -> quit the appNote the case. A lowercase r is hot reload. An uppercase R (Shift plus r) is hot restart. Mixing them up is the most common slip.
In an IDE, use the toolbar. VS Code shows a restart button (a circular arrow) in the debug toolbar, and Android Studio and IntelliJ have separate hot reload and hot restart buttons. The IDE's default keyboard shortcuts for these vary by version and platform, so check your keymap rather than relying on memory.
Hot Restart Compared With a Full Restart
A full restart means stopping the app and running flutter run again. It is the slowest option because the build toolchain runs again and the app is reinstalled on the device.
| Question | Hot reload | Hot restart | Full restart |
|---|---|---|---|
| Applies edited Dart code | Usually | Yes | Yes |
| Keeps `State` objects | Yes | No | No |
| Runs `main()` again | No | Yes | Yes |
| Re-runs `initState` | No | Yes | Yes |
| Applies native or build-file changes | No | No | Yes |
| Relative speed | Fastest | Fast | Slowest |
Hot restart reloads only Dart code. If you change AndroidManifest.xml, Info.plist, Gradle files, pubspec.yaml dependencies, or native plugin code, a hot restart will not pick them up. Those need a full restart.
Failure Modes and Edge Cases
Hot restart can fail or mislead you in a few situations.
- Compile errors. If the code does not compile, the restart is rejected and the app keeps running the old code. Read the error in the terminal, fix it, and restart again.
- Work in flight. Pending futures, timers, and open streams from the old program are discarded. If your app was partway through a network call, that call's result is lost. Start the action again after the restart.
- Native state. Dart objects reset, but native resources can persist. A platform channel's native side, a background service, or a camera session may still hold values from before the restart. If the app behaves oddly after a restart, a full restart clears this.
- Debugger breakpoints. Breakpoints generally stay, but the program stops at them again as the app starts over. That is expected.
- Build mode. Hot restart, like hot reload, needs a debug build. Profile and release builds are compiled ahead of time and do not support either action.
Choosing Quickly
Ask in order:
- Did I only change what a widget's
buildmethod returns? Try hot reload. - Did I change
initState,main, or a global or static initializer? Use hot restart. - Did I change native files, assets declared in
pubspec.yaml, or dependencies? Use a full restart. - Do I just want a clean slate? Use hot restart.
If a change seems ignored after a reload, move one step down the list before you debug the code. The edit is often correct and the action was too weak.
Exercise
You change int _count = 0; in a State class to int _count = 5; and press r. The screen still shows 0 after you reload. A teammate says your edit did not save. What is the more likely cause, and what do you do?
Show answer
The edit saved. A field initializer runs when the State object is created, and hot reload keeps the existing State object, so the new initial value is never applied. Press R for a hot restart, which creates a new State and runs the initializer again, and the screen will show 5.
Exercise
After a hot restart, which of these are reset and which are kept: a counter in a State field, a value saved with shared preferences on disk, a static field you edited, and the current navigation stack?
Show answer
The State field, the edited static field (now at its new initial value), and the navigation stack (back to the home route) are reset. The value saved on disk is kept, because it lives outside the Dart program.