Sub topic 7
Triggering Reload and Restart: `r`, `R`, and IDE Buttons
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, and understand the difference between StatelessWidget and StatefulWidget. You also need a Flutter project that runs in debug mode.
Flutter gives you three common ways to apply changes while developing:
| Action | How to trigger it | Existing state |
|---|---|---|
| Hot reload | Press `r` in the `flutter run` terminal or use an IDE button | Keeps state |
| Hot restart | Press `R` in the terminal or use an IDE button | Resets Dart state |
| Full restart | Stop the app and run it again | Rebuilds and starts the app again |
Start the Development Session
Open a terminal in your Flutter project and run:
flutter run
# In the terminal where flutter run is active:
# r -> hot reload
# R -> hot restart
# q -> quit the appThe flutter run command builds the application, installs it on the selected device, and keeps the terminal connected to the running app. The terminal accepts single-key commands while the process is active.
Use lowercase r for hot reload. Flutter sends updated Dart code to the running Dart virtual machine, then rebuilds the affected widget tree.
Use uppercase R for hot restart. Flutter starts the Dart application again, so Dart objects such as State instances are created from scratch.
The Case of the Key Matters
The two commands are different because the keys are different:
rmeans hot reload.Rmeans hot restart.qquits the running session.
If you press R when you intended to press r, your edits will still appear, but temporary values such as a counter, selected tab, or form contents will reset. This can make it harder to tell whether your code change or the restart caused a visible change.
Use IDE Controls
Android Studio and IntelliJ IDEA show Flutter run controls when the Flutter and Dart plugins are installed. Visual Studio Code shows Flutter controls when the Dart Code extension is installed.
The exact button labels can vary by IDE version, but the controls normally have these meanings:
- Hot Reload applies code changes while preserving the current state.
- Hot Restart reruns the Dart entry point and clears in-memory application state.
- Stop ends the current application process.
- Run starts a new process. Using Stop followed by Run is a full restart.
The IDE sends the same kinds of requests as the terminal. The button is a different interface, not a different Flutter mechanism.
A Practical Workflow
Use this loop while editing a widget:
- Start the app with
flutter run, or use the IDE's Run command. - Interact with the app so that it has visible state, such as a nonzero counter.
- Change a widget's layout, label, color, or other build-time code.
- Trigger hot reload with
ror the IDE's Hot Reload button. - Check the result without losing your interaction state.
- Trigger hot restart with
Ror the IDE's Hot Restart button when you needmainor initial setup code to run again.
For example, if a counter displays Count: 4, changing its label and hot reloading should leave the count at 4. Hot restarting recreates the page, so the count returns to its initial value.
When to Choose Each Action
Choose hot reload when the edit is inside code that Flutter can apply to the running program and you want to preserve the current screen state.
Choose hot restart when the edit changes startup behavior, initial values, or code that hot reload cannot apply correctly, but the application can still use the existing build.
Choose a full restart when you need to stop the process and rebuild or reinstall the application. This is also useful when a native plugin, platform configuration, asset, or build setting changed.
Exercise
The app shows a counter value of 7. You change the text in build and want to keep the counter at 7. Which action should you use?
Show answer
Use hot reload by pressing lowercase r or by clicking the IDE's Hot Reload button.