Sub topic 9
Choosing Reload, Restart, or Full Restart
This chapter assumes you can run an app with flutter run. It also assumes you know how StatefulWidget and State relate, and that you have already seen hot reload keep a counter's value. The earlier chapters explained how each mechanism works. This chapter covers how to pick one quickly and what to do when the first choice fails.
The Three Actions
Flutter gives you three ways to get an edit into a running app. They differ in how much they throw away.
| Action | What is kept | What is reset | Typical cost |
|---|---|---|---|
| Hot reload (`r`) | Dart VM, native host app, all `State` objects, global and static values | Nothing. Widgets are rebuilt with the new code. | Under a second or so |
| Hot restart (`R`) | Dart VM process and native host app | All Dart state. `main()` runs again and every `State` is recreated. | A few seconds |
| Full restart (stop, then `flutter run`) | Nothing | Everything, including the native app and the Dart VM | Tens of seconds to minutes |
The rule is to use the cheapest action that can apply your change. A cheaper action that cannot apply a change does not fail loudly every time. Sometimes it applies half of it, or none of it, and leaves you looking at stale behavior. The sections below help you spot that.
Match the Change to the Layer
Each action reaches a different layer of the app. Think of three layers:
- Widget code. The
buildmethods, styles, layout, and callbacks. - Dart initialization.
main(),initState, global variable initializers, static field initializers, and anything else that runs once at startup. - The native and build layer. Kotlin, Java, Swift, and Objective-C code, plugins, platform manifests, and the Gradle or Xcode configuration.
Hot reload reaches only layer 1. Hot restart reaches layers 1 and 2. A full restart is the only action that reaches layer 3. Most of the decision comes down to asking which layer the edit lives in.
Cases That Belong to Hot Reload
Use hot reload for edits to code that runs on every build or every event:
- Changing text, colors, padding, fonts, and layout inside
build. - Adding, removing, or reordering widgets in a
buildmethod. - Changing the body of an
onPressedcallback or any other method. - Adding a new method, a new widget class, or a new field with a default value used only in new code.
Hot reload keeps the existing State objects and calls build again, so you stay on the same screen with the same data. This is the main reason to prefer it when you are tuning a UI that took many taps to reach.
Cases That Need Hot Restart
Hot reload does not rerun initialization code. When your edit changes something that only runs at startup, the old value stays in memory. The following run only once, so edits to them need a hot restart:
- Values set in
initState. TheStateobject already exists, soinitStateis not called again. - The body of
main(). The app has already started. - Global variable and static field initializers. These are evaluated once, when first used. Changing
int globalCounter = 10;to20does not update a variable that already holds10. - Constructor-time setup of long-lived objects. Controllers, streams, and timers created earlier keep their old configuration.
Some structural changes also need a restart. Hot reload typically rejects or mishandles these:
- Changing an enum into a regular class, or the reverse.
- Changing a class into a mixin or similar kind changes.
- Changing the type arguments or declaration of a generic type.
- Changing a function type used by an existing field in a way that existing objects cannot accept.
When the tool cannot apply such a change, it prints a message saying a hot restart is required. Treat that message as the answer and press R.
Here is an example of how the two layers behave differently in one State class.
import 'package:flutter/material.dart';
int startingValue = 5;
void main() => runApp(const MaterialApp(home: DemoPage()));
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
late int _count;
@override
void initState() {
super.initState();
_count = startingValue;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}Run this and tap the button a few times. Then try three edits in turn:
- Change
'Count: $_count'to'Total: $_count'and pressr. The label changes and the number stays. This is a layer 1 edit. - Change
startingValueto100and pressr. Nothing changes. Both the global andinitStatehave already run. - Press
R. The app restarts,initStateruns again, and the count shows100.
Cases That Need a Full Restart
A hot restart reloads Dart code into the same running native app. It cannot change anything the native app was built with. Stop the app and run flutter run again after:
- Editing platform code in
android/orios/, such as a method channel handler in Kotlin or Swift. - Adding or upgrading a package that contains native code (a plugin). Adding any dependency to
pubspec.yamlalso calls for a rebuild. - Changing
AndroidManifest.xml,Info.plist, Gradle files, entitlements, or permissions. - Changing app icons, launch screens, or the app name.
- Changing the Flutter SDK version or the build configuration.
New assets are a gray area. Adding an entry under flutter: assets: in pubspec.yaml is often picked up by a hot restart, but if an image still fails to load, do a full restart before debugging further.
Reading the Console
After each action the flutter run console prints a line that tells you what happened:
- A message such as
Reloaded 1 of 600 librariesmeans the new code reached the VM. It does not say the visible result changed. - A message that a hot restart is required means the VM rejected the reload because of a structural change. The old code is still running.
- A compile error means the reload was rejected. The app keeps running the last good version, and your state is intact. Fix the error and reload again.
- A red error screen after a reload usually means the new
buildthrew an exception. Fix it and reload. The state is usually still there.
If a reload leaves the app in a broken state, such as a half-initialized late field or a null that cannot occur in normal startup, press R before concluding the code is wrong. Those failures often come from state created by an older version of the code.
Costs and Failure Modes
Hot reload's main risk is hidden stale state. You can keep editing for an hour while a State object holds values from an earlier version of the code, and then see a bug that a fresh start would not show. A good habit is to hot restart before you test a behavior you intend to commit, and do a full restart before a final check.
Hot restart's main cost is lost context. If you were three screens deep in a form, you are back at the home screen. Some teams add a debug-only route or saved state so the app can return to a screen quickly after a restart. Keep that out of release code.
Full restart's main cost is time, which pushes people to avoid it even when it is the right choice. If you changed a native file and the app still behaves the same way, the cheaper actions are not going to help. Run the full restart first.
Where It Applies
Hot reload and hot restart depend on the Dart VM's JIT compiler in debug mode. They are not available in profile or release builds, which use ahead-of-time compilation. That is another reason to test performance in profile mode and not compare it to the debug loop. Support also differs by platform. Check the Flutter documentation for your SDK version before relying on hot reload for web or desktop targets. The decision flow above is the same wherever hot reload works.
A Quick Checklist
- Did you only change how something looks or what a callback does? Press
r. - Did you change startup values,
main, global or static initializers, or the kind of a type? PressR. - Did you touch native code, plugins, manifests, or dependencies? Stop and run again.
- Did the first choice fail to show the change? Move one step up, in that order.
Exercise
You change int retries = 3; in a static field to int retries = 5; and press r. The app logs "Reloaded" but still uses 3. Which action should you take, and why did the first one not work?
Show answer
Press R to hot restart. Static field initializers are evaluated once when first used, and hot reload does not rerun them, so the field keeps its old value. A hot restart reruns the Dart program from main(), which re-evaluates the initializer.
Exercise
You add the url_launcher package to pubspec.yaml and run flutter pub get. Calls to the new package throw a missing plugin error after a hot restart. What should you do?
Show answer
Stop the app and run flutter run again. A plugin includes native code that has to be compiled and installed into the Android or iOS app. Hot restart reloads only Dart code into the existing native app.