Sub topic 5
What State Survives a Hot Reload
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, understand StatelessWidget and StatefulWidget, and can run an app in debug mode.
Hot reload updates Dart code in a running Flutter app. Flutter then rebuilds the widget tree. The important detail is that hot reload usually keeps the existing State objects. Fields inside those objects keep their current values.
The State Object Is the Boundary
A StatefulWidget has two related parts:
- The widget object describes configuration. Flutter may create a new widget object during a rebuild.
- The
Stateobject stores mutable values and usually remains mounted.
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),
),
);
}
}Start the app, tap the button several times, and change the text in build, such as changing Count: to Total:. After hot reload, the label changes but _count keeps its value.
The reason is that Flutter reuses the existing _CounterPageState object. Hot reload does not run createState again for this still-mounted page, and it does not reset _count.
What Usually Survives
The following values normally survive hot reload:
- Fields in an existing
Stateobject, such as_count. - Text entered into a mounted
TextField, because its controller or internal state remains mounted. - Scroll position in an existing scrollable widget.
- Selected tabs, expanded panels, and other values stored in mounted state objects.
- Values held by objects that the surviving state still references.
Flutter matches widgets in the old and new trees using their runtime type and location. Keys also affect this matching. If Flutter can match the old widget to the new widget, it keeps the old state.
| Edit during hot reload | Typical result |
|---|---|
| Change text returned by `build` | New text appears; state survives |
| Change a button's callback | New callback is used; state survives |
| Change a field value already stored in `State` | Existing value usually remains |
| Change a widget's key | Flutter may create new state |
| Remove and re-add a stateful widget | State may be discarded |
Initializers and `initState`
A common mistake is expecting a changed initializer to replace a value immediately:
class _CounterPageState extends State<CounterPage> {
int _count = 0;
@override
void initState() {
super.initState();
// This runs once when this State object is created.
}
@override
Widget build(BuildContext context) {
return Text('Count: $_count');
}
}If you change _count = 0 to _count = 100 and hot reload after the page has already been created, the existing state still contains its old value. The field initializer and initState ran earlier. A hot restart creates a fresh application and runs them again.
Use build for output that should respond to a hot-reloaded code edit. Use initState for one-time setup, not for values that you expect hot reload to recalculate.
> callout note > Hot reload preserves objects, not source-code intentions. If a value was already initialized in a surviving State object, changing the initialization code does not overwrite that value.
A Practical Prediction Rule
Before reloading, ask:
- Is the value inside an existing
Stateobject? - Will the widget keep the same type, key, and position in the tree?
- Does the edited code run during the next
build?
If the first two answers are yes, the value usually survives. If the widget is replaced, its state starts over. If the third answer is no, the visible result may not change until a hot restart or full restart.
Exercise
You tapped a counter until it displayed 5. You then change the text in build from "Count" to "Items" and hot reload. What happens? If you change the initial value from 0 to 10 and hot reload afterward, what happens?
Show answer
The label changes to "Items" and the counter remains 5. Changing the initial value does not replace the existing State object, so the counter still displays 5. A hot restart is needed for the new initial value to run.