Sub topic 6
Hot Restart: Resetting State Quickly
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, understand the difference between StatelessWidget and StatefulWidget, and can run an app with flutter run or an IDE.
Hot restart stops the current Dart application and starts it again with the updated source code. Flutter rebuilds the widget tree from the beginning. Existing State objects, text entered into fields, navigation history, and in-memory variables are lost.
Hot restart is useful when hot reload updates the code but the old state makes the result hard to understand.
What Hot Restart Resets
Consider this counter:
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 until the display shows 5. A hot reload keeps the existing _CounterPageState, so the display still shows 5. A hot restart creates a new _CounterPageState. The field initializer runs again, and the display returns to 0.
Hot restart also reruns main() and initialization code that runs as the Dart application starts. This makes it useful after changing startup configuration or the initial route.
How to Trigger It
With flutter run, type an uppercase R in the terminal where the app is running:
flutter run
# In the active flutter run terminal:
# r -> hot reload
# R -> hot restart
# q -> quitThe distinction matters. Lowercase r asks Flutter to apply changes while preserving the current state. Uppercase R restarts the Dart application and resets that state.
Most Flutter IDE integrations provide separate actions for Hot Reload and Hot Restart. In Visual Studio Code, use the Flutter commands in the Command Palette or the debug toolbar. In Android Studio or IntelliJ IDEA, use the reload and restart buttons in the Flutter run window. The exact button placement can vary by IDE version, so check the action label before selecting it.
Choosing Hot Restart
Use hot restart when:
- You changed an initial value and want the new value to be created.
- You changed startup logic in
main(). - The current navigation or widget state hides the effect of your edit.
- A plugin or service needs Dart-side initialization again.
- You want to reproduce the app's first-screen behavior without stopping the run session.
Hot restart is not enough for every change. Native Android or iOS code, native dependencies, platform configuration, and some build settings can require stopping the app and running it again. That is a full restart. If the Dart VM cannot apply a structural code change, Flutter may also tell you to perform a full restart.
A Practical Test
Run the counter example and tap the button several times. Change the label in build, then hot reload. The new label appears, but the count remains. Now change _count = 0 to _count = 100 and hot reload. The visible count may still be the old value because the existing State object was not recreated. Hot restart the app. The new initial value appears as 100.
This test separates two ideas:
- Hot reload applies many code changes to the running program.
- Hot restart recreates the Dart application so initial state is evaluated again.
Exercise
The counter shows 7. You change its field initializer from 0 to 50. Which action makes the counter start at 50?
Show answer
Hot restart. Hot reload keeps the existing State object, whose _count field already has its current value of 7.