Sub topic 11
Iterating with hot reload and hot restart
This chapter assumes you can run flutter create and flutter run, and that you have read the counter app from the previous chapters: a StatelessWidget root, a StatefulWidget page, and setState. You should also know that build() describes the UI and that Flutter calls it again when state changes.
Why Flutter can update a running app
In debug mode, Flutter runs your Dart code on the Dart VM with a just-in-time (JIT) compiler. A JIT compiler turns code into machine code while the app runs. Because the VM is still active, the Flutter tool can send it new code without stopping the process.
This gives you two fast ways to apply edits:
- Hot reload injects changed source code into the running app and rebuilds the widget tree. The app keeps its state.
- Hot restart discards the app's state and runs
main()again, but it does not rebuild the whole native app or reinstall it.
A third option, a full restart, stops the app and starts it again from scratch. Use it when neither hot option is enough.
Both hot options work only in debug mode. Profile and release builds use ahead-of-time (AOT) compilation, which compiles Dart to machine code before the app starts. Those builds cannot accept new code while running. The last chapter of this tutorial covers the build modes.
Using hot reload
Start the counter app with flutter run. The terminal stays attached to the running app and accepts single-key commands.
| Key | Action |
|---|---|
| `r` | Hot reload |
| `R` | Hot restart |
| `h` | List all available commands |
| `q` | Quit and stop the app |
In VS Code and Android Studio, saving a file can trigger hot reload automatically. VS Code reloads on save by default for Flutter debug sessions. In Android Studio and IntelliJ, use the lightning-bolt button, or enable the setting that reloads on save. The terminal always works.
Try it with the counter app. Tap the button a few times so the screen shows Taps: 3. Then change the theme seed color in MyApp:
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
),Press r. The colors change, and the screen still shows Taps: 3. The _CounterPageState object survived, so _count kept its value.
Now change the label in build() from 'Taps: $_count' to 'Count: $_count' and reload again. The text updates and the number is unchanged. This is the main benefit of hot reload: you can adjust a screen that took several steps to reach without repeating those steps.
What hot reload does internally
Hot reload works in three steps:
- The tool finds which source files changed since the last compile.
- It recompiles those libraries into a small kernel file (Dart's intermediate form) and sends it to the VM.
- The VM replaces the definitions of the changed classes and functions. Flutter then marks the widget tree as dirty and runs a new frame, which calls
build()on existing widgets.
Existing State objects stay alive, and their fields keep their values. The new code is used the next time build() runs. Code that does not run again is not affected, which causes the most common surprises below.
Changes hot reload does not apply
Hot reload replaces code but does not re-run code that already ran. Several kinds of edits will therefore look like they did nothing.
Initial values of state
Suppose you change int _count = 0; to int _count = 10; and hot reload. The screen still shows the old value. The field initializer ran once, when the State object was created, and that object still exists. Hot restart creates a new State, so the initializer runs again and the screen shows 10.
Code in main() and initState
main() runs only at startup. If you change what runApp receives, for example swapping const MyApp() for another root widget, hot reload will not show it. The same applies to initState(), which runs once per State object. If you add a line there, such as starting a timer or loading data, restart to see it.
Global variables and static fields
Flutter initializes top-level variables and static fields the first time they are read. A new initializer expression for one that was already read is ignored on reload. Restart to pick up the new value.
Enums and type changes
Hot reload cannot always convert existing objects when their type changes. These edits normally need a hot restart:
- Changing an enum into a regular class, or the reverse.
- Changing a class into a mixin, or the reverse.
- Changing a generic type's type parameters.
- Changing a
StatelessWidgetinto aStatefulWidget, which often works but sometimes leaves stale objects.
Adding or removing enum values may also need a restart. The tool will tell you when a reload was rejected, as shown below.
Native code and assets in some cases
Edits to Kotlin, Java, Swift, Objective-C, C++, or Gradle files are not Dart, so neither hot option applies them. Stop the app and run flutter run again. The same is true after you add a plugin with flutter pub add: plugins contain native code, so the app must be rebuilt. Adding a pure Dart package often works with a restart, but the safe habit is to stop and rerun after any change to pubspec.yaml.
Reading reload feedback
After you press r, the terminal prints a result. A successful reload looks like this:
Performing hot reload...
Reloaded 1 of 612 libraries in 438ms.The count shows how many libraries were affected. The first reload after startup is often slower because the tool has more to compile.
If your code has a compile error, the reload is rejected and the app keeps running the previous version:
Performing hot reload...
Try again after fixing the above error(s).Fix the error and press r again. You do not need to restart. When a reload cannot apply a change safely, you may see a message that the reload was rejected and that a hot restart is needed. Press R when that happens.
Using hot restart
Hot restart recompiles the whole app, sends it to the VM, and starts main() again. All state is lost: every State object is destroyed, global variables go back to their initial values, and the app returns to its first screen. It is still much faster than a full rebuild, usually taking a second or two, because the native shell is not rebuilt.
Use hot restart when you:
- Change initial values of fields.
- Edit
main()orinitState(). - Change global or static variables.
- Change enums or class kinds.
- Want to reset the app to a clean state after a long interaction.
Test the difference with the counter: set _count to 10, press r, and see the old value. Press R, and the screen shows Taps: 10.
When to use a full restart
Stop the app and run flutter run again when:
- You changed native files, Gradle or Xcode settings, or
AndroidManifest.xmlandInfo.plist. - You added or removed a plugin.
- You changed app icons or launch screens, which live in the native project.
- The app behaves oddly after many reloads and a hot restart does not fix it.
If dependency resolution seems out of date, run flutter pub get before restarting. If builds themselves misbehave, flutter clean removes the build output so the next run starts fresh. It is slower, so treat it as a last step.
A fast daily workflow
A practical loop looks like this:
- Run the app once and leave it running.
- Make a small edit and reload.
- If the change does not appear, restart.
- If the tool or the app reports a native or plugin problem, stop and rerun.
Keep edits small. When a reload fails, a small diff makes the cause obvious. Also keep your build() methods free of side effects such as network calls or timers. Reload re-runs build() often, so side effects there repeat. Put one-time work in initState(), and remember it needs a restart to change.
Edge cases worth knowing
- Reload while an async task runs. A reload does not cancel a running
FutureorStreamsubscription. Old callbacks may still fire, using the earlier code that was running when they were created, or the new code if they look up methods by name at call time. - Closures and callbacks. Functions that were already passed to another object, such as a stored
onPressedhandler, can keep running their earlier version until the widget rebuilds with the new function. constconstructors. Changing the arguments of aconstwidget works inbuild(), becausebuild()runs again. Changing aconstvalue held in a top-level constant may need a restart.- Web and desktop. Hot restart is available on all targets. Hot reload for Flutter web is supported in current releases, but if your version lacks it,
rfalls back to behavior closer to a restart. Checkhin the terminal to see what your target supports. - Debug only. If a hot key seems to do nothing, confirm you did not start with
--releaseor--profile.
Exercise
In the counter app you change int _count = 0; to int _count = 5; and press r. The screen still shows Taps: 0. Why, and what should you press to see 5?
Show answer
Hot reload keeps the existing _CounterPageState object, and the field initializer ran only when that object was created. Press R (hot restart), which destroys all state and runs main() again, so a new State is created with _count set to 5.
Exercise
You add the url_launcher plugin with flutter pub add url_launcher while the app is running, then press r. The new feature throws a missing plugin error. What is the cause, and what is the fix?
Show answer
A plugin contains native code, which hot reload and hot restart cannot install into the running app. Stop the app and run flutter run again so the native project is rebuilt with the plugin.
Exercise
You edit the text in build() from 'Taps: $_count' to 'Count: $_count' after tapping the button four times. Which command shows the new text and keeps the count at 4? Name the command that would reset the count.
Show answer
Hot reload (r) shows Count: 4, because it re-runs build() and keeps the State object. Hot restart (R) would reset the count to its initial value, 0.