Sub topic 6
Creating and Running Your First Flutter Project
Prerequisites
Assume that the Flutter SDK is installed, flutter doctor reports no blocking problems, and you have at least one configured run target. A run target is a device or platform where Flutter can launch your app, such as an Android emulator, an iOS simulator, Chrome, or a desktop system.
You should also be comfortable opening a terminal and changing directories. The commands in this chapter work on macOS, Linux, and Windows, although path syntax can differ.
Create the project
Open a terminal in the parent folder where you keep development projects. Run:
flutter create my_first_app
cd my_first_appflutter create generates a complete project. The final argument, my_first_app, becomes the project directory and the default Dart package name. Use lowercase letters and underscores for this name. For example, weather_tracker is valid, while Weather Tracker is not.
The generated project includes a starter application in lib/main.dart. It also includes platform folders and configuration files for the targets supported by your Flutter installation.
A project is created locally. The command does not publish it or connect it to an app store.
Understand the first run
Before launching the app, check which targets Flutter can see:
flutter devicesThe result lists available devices and their identifiers. If you have one target, this command is often enough:
flutter runIf several targets are available, specify one with -d:
flutter run -d chromeYou can replace chrome with the identifier shown by flutter devices, such as an emulator identifier or a desktop target.
Flutter compiles the starter app, installs it on the selected target, and attaches a development connection. The terminal displays logs and accepts commands while the app runs. Press q to stop the running session.
| Command | Purpose |
|---|---|
| `flutter create my_first_app` | Generates a new Flutter project |
| `flutter devices` | Lists detected run targets |
| `flutter run` | Builds and launches the app |
| `flutter run -d chrome` | Launches the app on Chrome |
Follow the launch flow
The relationship between the commands and the running application looks like this:
If flutter devices does not list the target you expect, do not repeatedly run flutter run. First start the emulator or simulator, connect the physical device, or repair the target configuration. Running flutter doctor can identify missing platform tools.
Try a clean first run
Run the following sequence from the project directory:
flutter create my_first_app
cd my_first_app
flutter devices
flutter runIf the project already exists, do not run flutter create over it just to launch it. Change into the existing directory and run flutter run. Keep the terminal session open while you explore the starter app. Later chapters will replace its generated screen with your own widget tree.
Exercise
You created my_first_app, but flutter run says that no device is available. What should you check first?
Show answer
Run flutter devices, start or connect a supported target, and run flutter doctor if the target or its platform tools are missing.