Sub topic 4
Hot Reload: Applying Code Without Losing Your Place
Prerequisites and the Core Idea
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, understand StatelessWidget and StatefulWidget, and can run an app with flutter run or an IDE.
Hot reload applies changed Dart code to a running Flutter app without restarting the app process. Flutter recompiles the changed libraries, updates the running Dart virtual machine, and rebuilds the widget tree. Existing State objects usually remain in memory.
That last point is the key difference from starting the app again. If a user has entered text, opened a screen, or incremented a counter, hot reload normally keeps that state.
flowchart TD
A[Edit Dart code] --> B[Save the file]
B --> C[Hot reload]
C --> D[Updated code is loaded]
D --> E[Widgets rebuild]
E --> F[Existing State objects remain]caption: Hot reload updates code while preserving the running widget state. alt: A flow from editing Dart code through hot reload and widget rebuilding to preserving existing state.
A Stateful Example
In this example, tapping the button changes _count. Editing the text inside build and then hot reloading changes the displayed label, but _count keeps its current value.
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),
),
);
}
}Run the app, tap the button several times, and change Count: to another label. Save the file and hot reload. Flutter runs build again with the new source, while the existing _CounterPageState object remains.
A rebuild is not the same as recreating state. Flutter compares the new widget tree with the previous tree and updates the parts that changed. A StatefulWidget can receive a new widget configuration while its associated State object continues to exist.
What Hot Reload Updates
Hot reload works best for changes that affect widget construction or rendering:
- Text, colors, padding, and layout changes inside
build - New widgets or changed widget properties
- Changes to methods called while building the current screen
- Changes to event-handler code that will run after the reload
The framework calls reassemble during hot reload. It then rebuilds the affected portions of the application. A reload does not normally call initState again, because the State object was not discarded.
A Practical Reload Check
Use this sequence when developing:
- Start the app in debug mode.
- Change a widget or method.
- Save the file.
- Trigger hot reload.
- Check both the changed UI and important state, such as the counter value.
If the new code is not visible, confirm that the file was saved and that the running app uses that file. Then check whether the edited method is reached by the current screen. A code path used only after navigation or a button press may not show its change immediately.
Hot reload does not rerun main, global initializers, or initState. Use hot restart when you need initialization code to run again. Use a full stop-and-run cycle when the change affects native platform code, build configuration, or another part that the debug Dart reload cannot replace.
Exercise
After incrementing a counter to 5, you change the text in build and perform a hot reload. What should happen?
Show answer
The new text should appear, and the counter should normally remain at 5 because the existing State object was preserved.