Sub topic 11
Iterating with Hot Reload and Hot Restart
This chapter assumes that you have Flutter installed, can run a generated project, and understand Dart classes and functions. You will use a running Flutter app to compare hot reload with hot restart.
Hot reload
Hot reload sends changed Dart code to a running application. Flutter then rebuilds the widget tree while preserving the existing State objects. This lets you change the interface without losing values entered or actions already taken.
Start the app from the project directory:
flutter runChange a widget's text, layout, color, or styling in your editor. Save the file, then press r in the terminal where Flutter is running. In an editor with Flutter support, use its Hot Reload command.
Consider this complete lib/main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(const CounterApp());
}
class CounterApp extends StatelessWidget {
const CounterApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
),
home: const 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('Hot reload demo')),
body: Center(
child: Text(
'Count: $count',
style: Theme.of(context).textTheme.headlineMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
count++;
});
},
child: const Icon(Icons.add),
),
);
}
}Tap the button several times. Change Colors.indigo to Colors.green, then hot reload. The color changes, but count keeps its current value. Flutter reused _CounterPageState instead of creating a new one.
Hot reload normally runs build() again. It does not rerun main() or call initState() again.
Hot restart
Hot restart stops and starts the Dart portion of the application. It recreates the widget tree and all State objects. In the example, count returns to 0.
Press R in the Flutter terminal, or choose Hot Restart in your editor.
| Action | Rebuilds widgets | Preserves State objects | Reruns `main()` |
|---|---|---|---|
| Hot reload | Yes | Yes | No |
| Hot restart | Yes | No | Yes |
| Full restart | Yes | No | Yes |
Use hot restart when initialization code changed, a global value is stuck, or hot reload cannot apply a structural change. Use a full restart when you changed native Android, iOS, desktop, or web configuration. Native code is compiled outside the Dart hot-reload cycle.
Common errors
Hot reload cannot fix every change. A compile error prevents the new code from loading. Read the error in the terminal, correct it, and reload again. If a changed class structure leaves the running app in an unexpected state, use hot restart.
Do not use hot reload as a test of startup behavior. Code that runs only in main() or initState() will not run again until a hot restart or full restart.
Exercise
After changing a widget's color, should you use hot reload or hot restart if you want to keep the current counter value?
Show answer
Use hot reload. It rebuilds the interface while preserving the existing State object and its counter value.