Sub topic 11
Running Code on Reload with `State.reassemble`
This chapter assumes you know basic Dart syntax, can build simple Flutter widgets, and understand the difference between StatelessWidget and StatefulWidget. It also assumes you can run an app with flutter run or an IDE.
What `reassemble` Does
State.reassemble is a lifecycle method that Flutter calls during hot reload. A lifecycle method is a method that the framework calls at a particular stage in an object's life.
Hot reload updates Dart code in the running application. Flutter then rebuilds the widget tree and calls reassemble on active State objects. The existing State objects keep their fields, so values such as counters, form text, and selections remain available.
Override reassemble when you need to run development-only code after every hot reload. Always call super.reassemble() first so Flutter can perform its normal work.
The method is useful for logging, refreshing a development-only value, or notifying a tool that code has changed. It is not a replacement for initState.
A Working Example
The following app logs the current counter value every time hot reload occurs. Run it, tap the button, change the text inside build, and press r in the terminal. The displayed text changes, but _count remains unchanged.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ReloadPage()));
}
class ReloadPage extends StatefulWidget {
const ReloadPage({super.key});
@override
State<ReloadPage> createState() => _ReloadPageState();
}
class _ReloadPageState extends State<ReloadPage> {
int _count = 0;
@override
void reassemble() {
super.reassemble();
debugPrint('Hot reload completed. Count is $_count.');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Reload Demo')),
body: Center(
child: Text(
'Current count: $_count',
style: const TextStyle(fontSize: 24),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}reassemble runs on the existing _ReloadPageState object. Flutter does not create a new object just because the source code changed. That is why _count survives.
A hot restart behaves differently. It restarts the Dart isolate, which is the running Dart execution environment. The old State object disappears, main runs again, and _count returns to zero. A full restart, where you stop and run the application again, also creates a fresh process and resets the state.
| Action | `reassemble` called on existing state? | Field values preserved? |
|---|---|---|
| Hot reload | Yes | Yes |
| Hot restart | No, old state is discarded | No |
| Full stop and run | No, old process is discarded | No |
Safe Uses and Common Mistakes
Use reassemble for debugging or development support. For example, debugPrint statements can show whether a reload reached a particular state object.
Do not put required production initialization in reassemble. It is primarily a debug-mode hook and is not a normal application startup callback. Put one-time initialization in initState, and put repeatable UI work in build or another suitable method.
Avoid assuming that every object in your application receives the callback. Only active State objects in the running widget tree can receive it. Also remember that changing an initializer in initState will not rerun initState during hot reload. Use hot restart when that initial value must be recreated.
Exercise
After tapping the button three times, you edit the text returned by build and press r. What should the log and counter show, and why?
Show answer
The log should report a count of 3, and the counter should remain 3. Hot reload calls reassemble and rebuilds the UI while preserving the existing State object.