Sub topic 2
The Development Loop with `flutter run`
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, understand the difference between StatelessWidget and StatefulWidget, and have run a Flutter app before.
A productive Flutter development loop has four steps:
- Edit Dart code.
- Start or connect to a running app.
- Apply the edit with hot reload or hot restart.
- Inspect the result and repeat.
The flutter run command starts this loop from a terminal. By default, it runs the app in debug mode, where Flutter connects the application to the Dart virtual machine and provides development features such as hot reload.
Start the app from the terminal
Run this command from the root directory of a Flutter project:
flutter runFlutter looks for a connected device, emulator, or simulator. If more than one target is available, specify one with -d:
flutter devices
flutter run -d chromeThe first command lists available targets. The second starts the app in a Chrome browser. Other common target names include an Android emulator, an iOS simulator, or a physical device.
After the app starts, the terminal stays attached to the running process. It displays status messages and accepts single-key commands.
r Perform a hot reload
R Perform a hot restart
q Quit the running app
h Show available commandsThe lowercase r and uppercase R are different commands. The terminal does not require you to press Enter after a command.
Follow the edit-and-check cycle
Suppose the app contains a counter page. Tap the button several times, then change the label in the build method:
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('Items: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}Save the file and press r in the terminal. Flutter reloads the changed Dart code and rebuilds the affected widget tree. The label changes to Items, while the current value of _count normally remains unchanged.
The important development habit is to make small edits and reload often. This makes it easier to connect a visible result with the code that caused it.
Understand the terminal workflow
The terminal process and the app are related but distinct:
flutter runstarts the app and maintains the debugging connection.- The running app displays the current Flutter interface.
- The terminal receives reload and restart commands.
- Your editor saves the source files that Flutter reloads.
This workflow can be represented as follows:
Use an IDE when preferred
Android Studio, IntelliJ IDEA, and Visual Studio Code provide buttons or keyboard shortcuts for hot reload and hot restart. The IDE still uses a Flutter debug session behind the scenes. Start the application in debug mode, edit and save a Dart file, then use the reload control.
The terminal is useful when you want visible command output or need to work without an IDE. An IDE is useful when you want integrated breakpoints, device selection, and editor actions. Both approaches use the same Flutter development model.
Check whether the loop is working
After a reload, read the terminal output. A successful reload usually reports that the reload completed. If the app does not change, first confirm that:
- You saved the file.
- You edited the file used by the running app.
- The terminal is attached to the expected project.
- The selected device is the one you are viewing.
- The change is one that hot reload can apply.
If the edit changes startup code or initial state, hot restart may be the appropriate next action. A full stop-and-run cycle is reserved for changes that the debug session cannot apply.
Exercise
You changed a widget's visible text, saved the file, and want to preserve the current counter value. Which command should you use, and why?
Show answer
Press lowercase r for hot reload. It applies the changed widget code while normally preserving the existing State object and its counter value.