Sub topic 6
Creating and running your first project
This chapter assumes that flutter doctor already reports a working toolchain and that you have at least one run target, such as a browser, a desktop, an emulator, or a phone. You should also be comfortable in a terminal. Here you will generate a project, choose the device it runs on, and learn what happens during the first build. The files in the generated folder are covered in the next chapter.
Generating a project with flutter create
The flutter create command writes a complete, runnable project into a new folder. Run it from the directory where you keep your code:
flutter create my_first_app
cd my_first_app
flutter runThe command does four things:
- It creates a folder named after the project.
- It copies the template files into it: Dart sources, a test, platform folders, and configuration.
- It runs
flutter pub getto download the dependencies the template declares. - It prints the commands to try next.
The flutter run command then builds the app for a device and launches it. The template is a counter app with a button and a number. It works on every platform you have enabled, so you can check the whole toolchain before you write any code.
Project naming rules
The project name becomes the Dart package name, so it must follow Dart's rules.
- Use only lowercase letters, digits, and underscores.
- Start with a letter or underscore, not a digit.
- Do not use a Dart reserved word such as
classorswitch. - Do not reuse the name of a package the template depends on, such as
flutterortest.
The name My-App fails because of the uppercase letter and the hyphen. Use my_app instead.
Options worth knowing
The defaults are fine for a first project. These flags change the result in ways you will meet in real work:
| Flag | Effect |
|---|---|
| `--org com.example` | Sets the reverse-domain organization prefix used for the Android application ID and the iOS bundle identifier. The default is `com.example`. |
| `--platforms=android,ios,web` | Generates platform folders only for the listed targets. |
| `--project-name my_app` | Sets the package name when it should differ from the folder name. |
| `--empty` | Generates a minimal `main.dart` instead of the counter template. |
| `--template=package` | Creates a reusable Dart/Flutter package instead of an app. Other templates include `plugin` and `module`. |
| `.` as the target | Creates the project inside the current directory and uses the directory name as the package name. |
A command that combines several of these looks like this:
flutter create --org com.mycompany --platforms=android,ios,web --empty notes_appThe organization prefix matters more than it looks. Store listings use the application ID to identify an app, and changing it later means editing several platform files. Set --org to a domain you own before you generate the project.
The --platforms flag is not permanent. You can add a platform later from inside the project:
flutter create --platforms=windows .This adds the missing platform folder and leaves your Dart code alone.
Choosing a device for flutter run
Before you run, list what Flutter can see:
flutter devicesEach line shows a device name, an ID, a platform, and an OS version. The ID is what you pass to -d:
flutter run -d chrome
flutter run -d macos
flutter run -d emulator-5554The exact IDs depend on your machine. Android emulators usually appear as emulator-5554, and a physical phone appears under its serial number. If only one device is available, flutter run uses it. If several are available, it lists them and asks you to choose. In scripts, always pass -d so the command never waits for input.
The first launch of each target takes longer than later ones. The reasons differ by platform:
- Android: Gradle downloads its distribution and the Android dependencies, and then compiles the app.
- iOS and macOS: CocoaPods installs native dependencies, and Xcode compiles the app.
- Web: the Dart compiler builds JavaScript and starts a local development server.
- Windows and Linux: the native toolchain compiles the runner and links it with the Flutter engine.
A first Android build can take several minutes. Later builds reuse caches and finish much faster, especially once hot reload is working, which the later chapter on hot reload covers.
The running session
When the build finishes, the app opens on the device and the terminal stays attached. The terminal prints a link to the Dart VM service, which developer tools use to connect. It also accepts single-key commands. Press h to see the full list. These are the ones you will use first:
rapplies code changes without losing app state (hot reload).Rrestarts the app and resets its state (hot restart).qstops the app and ends the session.ddetaches the terminal and leaves the app running.
Hot reload and hot restart get a full chapter later. For now, press r once after a small edit to confirm the connection works.
Run the app, tap the plus button a few times, then press q. If the number changes on each tap, your toolchain, device, and project all work.
Making the first edit
Open lib/main.dart in your editor. The template contains comments and several classes, which is more than you need to start. Replace the whole file with this minimal version:
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello, Flutter'),
),
),
),
);
}Save the file and press r in the terminal. The screen changes to the new text. Because the file no longer defines a MyApp class, the generated test/widget_test.dart no longer compiles against it. You will fix that when you write your own test. For now, run only the app and do not run flutter test.
Common first-run problems
Most failures at this stage come from the environment, not from the project. Match the symptom to the likely cause:
| Symptom | Likely cause | What to try |
|---|---|---|
| `No devices found` | No emulator is running, the phone is not authorized, or the target platform is disabled. | Start an emulator, accept the USB debugging prompt on the phone, or run `flutter config --enable-web-linux-etc` for your platform. Then run `flutter devices` again. |
| `Target of URI doesn't exist` or `Couldn't resolve the package` | Dependencies were not downloaded. | Run `flutter pub get` inside the project folder. |
| `Error: No pubspec.yaml file found` | You ran the command from the wrong folder. | Use `cd my_first_app` first. The command must run at the project root. |
| Gradle or SDK license errors on Android | Android licenses have not been accepted. | Run `flutter doctor --android-licenses` and accept them. |
| The build hangs on the first run | A first Gradle or CocoaPods download is in progress. | Wait, and check your network connection before you cancel. |
| Port or lock errors from a previous session | A stale `flutter` process holds the build lock. | Quit the old session, then run again. |
If a problem persists, go back to flutter doctor -v and read it again. It reports most environment faults, and the chapter on fixing setup problems goes through the harder cases.
Checklist before moving on
You are ready for the next chapter when all of these are true:
flutter createproduced a folder without errors.flutter deviceslists at least one device.flutter runlaunches the counter app on that device.- An edit to
main.dartfollowed byrchanges the screen.
Exercise
You generated a project with flutter create my_first_app, and flutter devices shows both a Chrome entry and an Android emulator. You want to launch on Chrome from a script without any prompt. Which command do you run, and why does it need that extra argument?
Show answer
Run flutter run -d chrome. With more than one device available, plain flutter run stops and asks you to choose, which blocks a script. The -d flag selects a device by ID, so no prompt appears. Using the ID shown by flutter devices (here chrome) works the same way.
Exercise
A teammate wants an app that will be published under the domain acme.dev, with only Android and web targets, starting from a minimal main.dart. Write the flutter create command for a project called field_notes. Then say how they could add iOS support later.
Show answer
flutter create --org dev.acme --platforms=android,web --empty field_notes. The organization is the reverse of the domain, which gives dev.acme. To add iOS later, run flutter create --platforms=ios . from inside the project folder. This adds the ios folder without changing existing Dart code.