Sub topic 2
The Development Loop with flutter run
This chapter assumes you can write basic Dart, build simple widgets, and tell a StatelessWidget from a StatefulWidget. It also assumes you have run an app with flutter run or from an IDE. Here you will look at what flutter run does, what stays alive while you work, and how an edit travels from your editor to the running app. Later chapters cover what reload and restart do to state.
What flutter run actually does
flutter run is a tool that builds your app, installs it on a device, starts it, and then stays attached to it. The attachment is what makes the development loop possible. After the first launch, the command does not exit. It keeps a connection to the running app and waits for your input.
In order, flutter run does the following:
- Picks a target device. With one device available it uses that one. With several, it asks you, or you pass
-d. - Builds the app in debug mode. On Android this runs Gradle. On iOS it runs Xcode build tooling. For the web it compiles to JavaScript.
- Installs and launches the app on the device.
- Connects to the Dart VM inside the app through the VM service, a debugging protocol the VM exposes.
- Starts a long-lived Dart compiler process and keeps it ready for incremental compiles.
- Waits for keypresses in the terminal.
The first build is the slow part, often tens of seconds or more. Everything after it is meant to be fast.
flutter devices
flutter run -d <device-id>flutter devices lists the device IDs you can pass to -d. Examples are an emulator ID, chrome, or macos.
Why this loop is fast: debug mode and the Dart VM
A debug build runs your Dart code on the Dart VM using JIT (just-in-time) compilation. The VM compiles code while the app runs, and it can accept new code while the program is alive. Release builds work differently. They compile your Dart ahead of time (AOT) into native machine code, and that code cannot be replaced while it runs.
This is why hot reload exists only in debug mode. The tooling depends on the VM being able to load new class and function definitions into a running program.
| Build mode | Command | Compilation | Hot reload and restart |
|---|---|---|---|
| Debug | `flutter run` (default) | JIT on the Dart VM | Available |
| Profile | `flutter run --profile` | AOT | Not available |
| Release | `flutter run --release` | AOT | Not available |
A debug build is also larger and slower than a release build. It includes debugging services and assertions. Do not judge performance from it. Use profile mode for that, and expect the reload keys to be missing there.
The path of one edit
When you press r for a hot reload, several pieces cooperate:
- The Flutter tool checks which source files changed since the last compile.
- The incremental compiler (the frontend server) recompiles only the affected libraries. It produces a small update to the program in Dart's kernel format.
- The tool sends that update to the Dart VM through the VM service.
- The VM replaces the definitions of the changed classes and functions. Existing objects stay in memory.
- The tool asks the Flutter framework to rebuild the widget tree so the new code runs.
The terminal then prints a summary. Your numbers will differ:
Performing hot reload...
Reloaded 1 of 640 libraries in 412ms (compile: 38 ms, reload: 190 ms, reassemble: 120 ms).The phrase "1 of 640 libraries" shows that only the changed library was reloaded. The three timings match the stages above. They are a useful first diagnostic. If the summary says Reloaded 0 libraries, the tool found no changes to send. A later chapter covers that case.
Keys in the flutter run terminal
While flutter run is attached, the terminal accepts single-key commands. Click into that terminal first, since the keys only work when it has focus.
flutter run
# In the terminal where flutter run is active:
# r -> hot reload
# R -> hot restart
# h -> list all available commands
# c -> clear the terminal screen
# d -> detach (leave the app running, stop the tool)
# q -> quit and stop the appKeys are case-sensitive. Lowercase r and uppercase R do different things. Mixing them up is the most common mistake in this loop. Press h to see the full list for your Flutter version. It also includes debugging toggles such as p for debug paint and o for switching the simulated platform.
The difference between d and q matters. q stops both the tool and the app. d detaches the tool and leaves the app running on the device. You can attach again later with flutter attach.
IDE integration
Android Studio, IntelliJ, and VS Code run the same tool underneath. Their buttons send the same commands to the same flutter run process:
- The lightning bolt button is hot reload.
- The circular arrow button is hot restart.
- The red square stops the app.
IDEs also add one convenience. With the Flutter plugins, saving a file triggers a hot reload by default. In a bare terminal, saving does nothing until you press r. If you edit in one program and run in another terminal, you must press r yourself.
Triggering from outside the terminal
On macOS and Linux you can tell a running flutter run to reload by sending it a signal. Start it with a PID file:
flutter run --pid-file /tmp/flutter.pidThen, from another terminal or a file-watcher script:
kill -USR1 "$(cat /tmp/flutter.pid)" # hot reload
kill -USR2 "$(cat /tmp/flutter.pid)" # hot restartThis lets you wire reload to a file watcher when you use an editor with no Flutter plugin. Windows does not support these signals.
A worked loop
Use this counter app to practice the loop. Run it, tap the button until the count reaches 3, and then edit the code.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: CounterPage()));
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}Try these steps in order:
- Run
flutter runand tap the button three times. The screen showsCount: 3. - Change
'Count: $_count'to'Taps: $_count'and pressr. The label changes toTaps: 3. The number stays because the existingStateobject, with its_countfield, was not replaced. - Press
R. The app restarts and showsTaps: 0. A hot restart creates the app state again frommain. - Tap twice more, then press
qand runflutter runagain. The result is the same as in step 3 (Taps: 0), but it took much longer. That gap is the reason the faster actions exist.
This shows the pattern the whole tutorial builds on. Your edit reached the running program within a second or so, and the state survived or reset depending on which key you pressed.
The three actions compared
The loop gives you three levels of reset. They differ in how much of the running program survives.
| Action | Trigger | App state | Dart code updated | Typical cost |
|---|---|---|---|---|
| Hot reload | `r` | Kept | Yes, in place | Under a second |
| Hot restart | `R` | Reset | Yes, whole app reruns from `main` | A few seconds |
| Full restart | `q`, then `flutter run` | Reset | Yes, and native code is rebuilt | Tens of seconds or more |
Hot restart still uses the same running app process, so it does not rebuild the native part of the app. A full restart does. If you change native code, plugin setup, or the platform project files such as AndroidManifest.xml or Info.plist, only a full restart picks it up. The same applies to adding a package with native code, so you must stop and run again after changing pubspec.yaml.
Common mistakes in the loop
- Pressing keys in the wrong window. The keys go to the terminal running
flutter run, not to your editor or the app. - Running a profile or release build and wondering where reload went. Check the mode you started with.
- Expecting a terminal save to reload. Only IDE plugins do that. In a plain terminal, press
r. - Using
Rwhenris enough. This resets state and costs you the screen you were tuning. - Using
rwhen the change is inmainor an initial value. The app looks unchanged. UseR. - Editing a file the app does not use. Dart files outside the app's import graph are never compiled into it, so a reload shows no change.
Exercises
Exercise
You run flutter run --release, edit a widget's text, and press r. Nothing updates. What is the cause, and which command should you use for fast iteration?
Show answer
Release builds are compiled ahead of time (AOT) and the Dart VM cannot accept new code, so hot reload and hot restart are not available. Use the default debug mode with flutter run, and use profile mode only when you measure performance.
Exercise
You use the counter app. The count is 5, and you change the title text in build from 'Counter' to 'Tap Counter'. You press r, and then R. What does the screen show after each key?
Show answer
After r, the title reads 'Tap Counter' and the count is still 5, because the existing State object is kept. After R, the title still reads 'Tap Counter' and the count is 0, because the app reruns from main and creates new state.
Exercise
You edit your code in a separate editor with no Flutter plugin and save the file. The running app does not change. Name two ways to apply the edit without leaving the terminal workflow.
Show answer
Press r in the terminal running flutter run. Alternatively, start flutter run with --pid-file and send kill -USR1 to that process ID (macOS and Linux) so a file watcher can trigger the reload.