Sub topic 7
Tour of the generated project
This chapter assumes you have installed the Flutter SDK, flutter doctor reports no blocking problems, and you have run flutter create my_first_app once. You should be comfortable reading a directory tree and a YAML file. The goal is to know what each generated file does, which ones you will edit often, and which ones you should leave alone.
What `flutter create` produces
Open my_first_app in your editor. The exact set of folders depends on your Flutter version and on which platforms you enabled, but a default project looks like this:
my_first_app/
├── android/
├── ios/
├── linux/
├── macos/
├── web/
├── windows/
├── lib/
│ └── main.dart
├── test/
│ └── widget_test.dart
├── pubspec.yaml
├── pubspec.lock
├── analysis_options.yaml
├── .gitignore
├── .metadata
└── README.mdTwo groups of files matter here. The first group is your own Dart code and configuration: lib/, test/, and pubspec.yaml. You edit these every day. The second group is platform folders such as android/ and ios/. These hold native host projects that Flutter generated for you. You touch them only for platform-specific settings such as permissions, app icons, or app identifiers.
The diagram shows the idea: your Dart code in lib/ is the single shared source. Each platform folder is a thin native wrapper that hosts the compiled Flutter engine and your code.
The `lib/` folder
lib/ holds all of your application's Dart code. A new project contains only lib/main.dart. As the app grows you add files and subfolders here, such as lib/screens/ or lib/widgets/. Flutter does not require a particular layout, so the structure is your decision.
Dart finds your code through the main() function in lib/main.dart. The tooling treats this file as the entry point by convention. You can run a different entry file with flutter run -t lib/other_main.dart, which is how some teams create separate staging and production entry points.
Files under lib/ are importable by package name. The project name from pubspec.yaml becomes the package name, so a file at lib/screens/home.dart is imported like this:
import 'package:my_first_app/screens/home.dart';Relative imports also work inside lib/, but pick one style and keep to it. The widget test in the next chapters uses the package: form, which is why renaming a project requires updating the name field in pubspec.yaml and every import that mentions it.
`pubspec.yaml`
pubspec.yaml is the project manifest. It names the package, declares the Dart SDK range it supports, lists dependencies, and registers assets such as images and fonts. A trimmed version of a generated file looks like this:
name: my_first_app
description: "A new Flutter project."
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ^3.5.0
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: trueYour SDK constraint and package versions will differ. Read through the fields in this order:
| Field | Purpose |
|---|---|
| `name` | The package name used in `package:` imports. It must be a valid Dart identifier in lowercase with underscores. |
| `publish_to: 'none'` | Prevents an accidental `pub publish` of an app. Apps are not published to pub.dev. |
| `version: 1.0.0+1` | The user-facing version, then a `+` and the build number. Mobile stores read both. |
| `environment.sdk` | The Dart SDK versions the project accepts. |
| `dependencies` | Packages your app needs at run time. |
| `dev_dependencies` | Packages used only for development, such as test and lint tools. |
| `flutter.uses-material-design` | Includes the Material icon font so `Icons.add` renders. |
Indentation is part of the syntax
YAML uses spaces for structure. Use two spaces per level and never tabs. A mis-indented sdk: flutter line is a common cause of a confusing "Error on line N" message from flutter pub get.
Dependencies and assets
You normally add a dependency with flutter pub add http, which edits the file for you and downloads the package. You can also edit the file by hand and run flutter pub get. Both end up in the same place. The pub add approach picks a compatible version constraint for you, so it is the safer default.
Assets are declared under the flutter: key. This example registers one folder:
flutter:
uses-material-design: true
assets:
- assets/images/The trailing slash includes every file directly inside the folder, but not files in its subfolders. Create the folder before you run the app, because a missing path is an error at build time.
`pubspec.lock` and `.dart_tool/`
pubspec.lock records the exact version of every package, including transitive ones, that the last pub get resolved. pubspec.yaml says ^1.0.8, which is a range. The lock file says 1.0.8, which is what you actually got.
For an application, commit pubspec.lock to version control. Everyone on the team and your build server then resolve the same versions. Library packages published to pub.dev traditionally do not commit it.
The hidden .dart_tool/ folder holds generated package configuration and build caches. It is listed in .gitignore. If tools behave strangely after a SDK upgrade, flutter clean followed by flutter pub get rebuilds these files.
`test/`
The test/ folder holds Dart tests that run with flutter test. The generated widget_test.dart tests the counter app that older templates produced. When you replace lib/main.dart with your own code, that default test can fail because it looks for text that no longer exists. Either update the test or delete it. A later chapter covers writing a widget test properly.
`analysis_options.yaml`
This file configures the Dart analyzer. The generated version includes the flutter_lints rule set, which flags style problems and likely mistakes in your editor and in flutter analyze:
include: package:flutter_lints/flutter.yaml
linter:
rules:
# avoid_print: falseYou can enable or disable individual rules under linter: rules:. Treat analyzer warnings as useful feedback. For example, the prefer_const_constructors lint is why the sample code writes const Text('Counter') and const MyApp(). A const widget is built once and reused, which saves work when the parent rebuilds.
Platform folders
Each platform folder is a normal native project that Flutter configures for you.
android/is a Gradle project. App identifiers and permissions live inandroid/app/src/main/AndroidManifest.xmlandandroid/app/build.gradle(orbuild.gradle.kts).ios/andmacos/contain Xcode projects. Settings such as the bundle identifier andInfo.plistentries are there. Building for these needs a Mac with Xcode.web/holdsindex.html, the web manifest, and icons. Flutter injects the compiled app intoindex.html.windows/andlinux/contain CMake-based runner projects.
The edits you will most likely make early are the app name, the identifier, and permissions such as camera or location access. Keep those changes small and commit them, because a later flutter create . run in the same folder can regenerate missing platform files and you want clean diffs.
You can add a platform you skipped at any time:
flutter create --platforms=web,windows .The command only adds the missing folders and leaves your lib/ code alone.
Files you should not edit by hand
.metadatarecords which Flutter version created the project and is used by the upgrade tooling..dart_tool/andbuild/are generated output.- Files inside
android/.gradle/andios/Pods/are managed by their build tools. pubspec.lockis produced bypub. Change it by runningflutter pub upgradeorflutter pub add, not by typing in it.
Most of these are already ignored by .gitignore. Check that build/ and .dart_tool/ are listed there before your first commit.
Common mistakes
- Putting code outside
lib/. Dart files outsidelib/cannot be imported with apackage:path. Keep application code inlib/. - Naming the project with a reserved word or a hyphen.
flutter create my-appis rejected. Usemy_app. - Forgetting
flutter pub getafter hand-editingpubspec.yaml. Editors often run it for you on save, but the command line does not. Unresolved imports are the usual symptom. - Declaring an asset but not creating the file. The build fails with a message naming the missing path.
- Assuming
pubspec.yamlranges are exact. Checkpubspec.lockfor the version actually in use.
Exercise
You add assets/logo.png to your project and list - assets/logo.png under flutter: assets: in pubspec.yaml. The app still reports that it cannot find the asset after a hot reload. Name two things to check.
Show answer
First, confirm the file exists at exactly assets/logo.png relative to the project root, with matching case, and that the assets: entry is indented under flutter: with spaces. Second, run flutter pub get and then fully restart the app (hot restart or a new flutter run), because asset changes are bundled at build time and hot reload does not pick up a changed asset list.
Exercise
Why is pubspec.lock usually committed for an app but not for a published library?
Show answer
An app needs repeatable builds, so committing the lock file makes every developer and the build server use identical package versions. A library is consumed by other projects that resolve their own versions, so its lock file would be ignored and only creates noise.