Sub topic 8
Changes Hot Reload Cannot Apply
This chapter assumes you can write basic Dart and build StatefulWidgets. It also assumes you have used flutter run with r and R, and know that hot reload keeps state while hot restart resets it. Here you learn which edits hot reload does not apply, and why.
Why Hot Reload Has Limits
Hot reload does not rebuild your app. Flutter sends the changed Dart source to the Dart VM, which runs in debug mode with JIT (just-in-time) compilation. The VM replaces the method bodies of the classes you changed. Flutter then calls reassemble, which marks the whole widget tree for rebuilding. Every build method runs again with the new code.
Hot reload therefore does two things: it swaps code, and it rebuilds widgets. It does not do these things:
- It does not re-run code that ran once and finished.
- It does not recompute values already stored in memory.
- It does not change anything outside the Dart VM, such as native code or platform configuration.
Almost every limit in this chapter follows from one of these three facts. If an edit only changes what a build method returns, hot reload applies it. If an edit changes something that ran earlier and is now stored, hot reload leaves the stored value alone.
Category 1: Code That Does Not Run Again
This is the most common source of "my change did nothing". The new code is loaded, but nothing calls it.
initState and other one-time lifecycle code
initState runs once, when Flutter creates a State object. Hot reload keeps the existing State object, so initState is not called again. The same applies to didChangeDependencies on first use and to anything you set up in a constructor.
main
main ran once when the app started. Editing it, for example changing the root widget passed to runApp or adding a service initialization call, has no effect on the running app. Hot restart re-runs main.
Global variable and static field initializers
Dart initializes top-level variables and static fields lazily, the first time you read them, and then keeps the value. Hot reload treats them as state. If you change the initializer of a variable that has already been read, the old value stays.
The following app shows all three cases. It prints a log line from main, copies a global into state in initState, and uses a static field as the step size.
import 'package:flutter/material.dart';
int startValue = 10;
class Config {
static int step = 1;
}
void main() {
debugPrint('main ran');
runApp(const MaterialApp(home: CounterPage()));
}
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
late int _count;
@override
void initState() {
super.initState();
_count = startValue;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Count: $_count (step ${Config.step})')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count += Config.step),
child: const Icon(Icons.add),
),
);
}
}Try these edits one at a time while the app runs:
- Change
startValue = 10to20and pressr. The screen still shows 10, becauseinitStatedid not run again andstartValuealready holds 10. - Change
static int step = 1to5and pressr. The step stays 1, because the static field was already initialized. - Add a
debugPrinttomainand pressr. Nothing prints. - Press
Rafter any of these edits. The log showsmain ran,startValueis 20, the step is 5, and the count starts at 20.
Edits to build in the same file, such as changing the label text, apply on r, because build runs again.
Callbacks and objects created earlier
Hot reload replaces method bodies, so closures defined in a method usually pick up new code the next time they run. A closure you already stored may still point at old behavior. The same goes for objects built in initState, such as a Timer, a StreamSubscription, an AnimationController with a fixed duration, or a TextEditingController with initial text. Their configuration was captured when they were created, so changing the arguments in initState does not update them.
Category 2: Changes to the Shape of Types
Hot reload can add methods and fields to a class and change method bodies. Some structural edits are harder for the VM to reconcile with live objects already on the heap. Flutter may reject the reload, or accept it and leave you with confusing state. The usual cases are:
- Changing an
enuminto a regular class, or the reverse. - Changing a class's type parameters, for example
BoxtoBox<T>. - Changing a field's type while instances of the class exist.
- Changing the superclass of a class, or turning a class into a mixin.
- Replacing a
constvalue with a non-const one, or the reverse, where existing objects were built as constants.
When the VM cannot reconcile the change, the terminal prints a message such as "Hot reload rejected" or "Try performing a hot restart instead". Follow that advice. A hot restart discards all objects and builds them fresh from the new definitions.
Some structural edits are accepted but leave an existing State object in an odd shape. For example, you might turn a StatelessWidget into a StatefulWidget. If the screen looks wrong afterward, restart before you debug further.
Category 3: Everything Outside the Dart VM
Hot reload only touches Dart code. These edits need a full restart, which means stopping flutter run and starting it again:
| Edit | Why hot reload cannot apply it | What to do |
|---|---|---|
| New or changed packages in `pubspec.yaml` | Dependencies must be fetched and compiled in | Run `flutter pub get`, then do a full restart |
| New asset or font entries | The asset bundle is built into the app | Full restart |
| Android native code, `AndroidManifest.xml`, Gradle files | The native app must be rebuilt and reinstalled | Full restart |
| iOS native code, `Info.plist`, Podfile | Same, for the iOS app | Full restart |
| Plugin native code or newly added plugins | Plugin registration happens at app launch | Full restart |
| App icon, launch screen, permissions | These live in the platform project | Full restart |
A hot restart does not help with these either. It still reuses the installed native app and the old bundle.
Category 4: Compile Errors
If your edit does not compile, hot reload applies nothing. The terminal and IDE show the error, and the app keeps running the last good code. Fix the error and reload again. The state you built up is still there, because the failed reload changed nothing.
A related trap is a warning-only message. The reload can succeed while the terminal reports a runtime exception from the rebuild. In that case the new code did load, but a build method threw. The red error screen replaces the widget, and fixing the bug and reloading usually restores it.
Predicting the Outcome
Before you press a key, ask three questions about the line you edited:
- Does something call this code again after a reload? Only
buildand the methods it calls are guaranteed to re-run. - Is the old result stored somewhere? State fields, globals, static fields, and controllers keep their old values.
- Does the edit leave the Dart VM? If so, only a full restart can apply it.
| You edited | Hot reload (`r`) | Hot restart (`R`) | Full restart |
|---|---|---|---|
| Text or layout in `build` | Applies, state kept | Applies, state reset | Not needed |
| A method called from `build` | Applies | Applies | Not needed |
| Value assigned in `initState` | Not applied to live State | Applies | Not needed |
| Body of `main` | Not applied | Applies | Not needed |
| Global or static initializer (already read) | Not applied | Applies | Not needed |
| Enum to class, generic parameters | Often rejected | Applies | Not needed |
| `pubspec.yaml` dependencies or assets | Not applied | Not applied | Required |
| Native or platform files | Not applied | Not applied | Required |
Making an Unapplied Change Visible
When you cannot tell whether a reload re-ran your code, add a log line to the method you suspect. State.reassemble runs on every hot reload, so it confirms that a reload happened and that the existing State object survived.
@override
void reassemble() {
super.reassemble();
debugPrint('Hot reload happened, _count is still $_count');
}If you see this line but your initState change has no effect, the reload worked and the edit belongs to category 1. If you do not see the line at all, the reload did not reach your State. Check for a compile error, or whether the app is running in debug mode.
A Practical Habit
Start with r, because it is fast and keeps your place. If the result is not what you expect, press R before you spend time debugging. A hot restart takes a second or two and removes stale state as a cause. If R also shows the old behavior, the edit is probably outside Dart, so stop the app and run flutter run again.
Exercise
You change _count = 0; to _count = 100; inside initState, save, and press r. The screen still shows the old count. Why, and what do you press to see 100?
Show answer
Hot reload keeps the existing State object and does not call initState again, so _count keeps its old value. Press R (hot restart), which discards all state, runs main, and creates a new State whose initState assigns 100.
Exercise
You add an image under assets/ and list it in pubspec.yaml. After r and then R, Image.asset throws an error that the asset cannot be found. What is the fix?
Show answer
Assets are packaged into the app bundle when it is built, and neither hot reload nor hot restart rebuilds that bundle. Stop the app and run flutter run again (a full restart) so the new asset is included.
Exercise
A global final List<String> items = ['a', 'b']; is already in use. You change it to ['a', 'b', 'c'] and press r. Does the list gain a third item? Give a reason.
Show answer
No. Hot reload treats an already initialized global as state, so the old list stays in memory and the initializer does not run again. Use hot restart to re-run the initializer.