Sub topic 2
Where dependencies live: pubspec.yaml
Every Flutter project has a file named pubspec.yaml at its root. It describes your project and lists the packages it needs. This chapter explains how that file is laid out, what the dependency entries mean, and which mistakes most often break it.
This chapter assumes you can read basic YAML, have created a project with flutter create, and can run commands in a terminal. Commands such as flutter pub add, version constraint rules, and pubspec.lock get their own chapters. Here the focus is the file itself.
What pubspec.yaml is for
The pubspec is the manifest of a Dart or Flutter package. Three tools read it:
- The pub tool reads it to decide which packages to download.
- The Flutter tool reads the
flutter:section to find assets, fonts, and other build settings. - pub.dev reads it when you publish a package, to show the name, description, and version.
Your app is itself a package, even if you never publish it. That is why an app has a name, a version, and a dependency list like any library.
The top-level fields
A pubspec is one YAML map. Its keys are fixed by the pub tool, and unknown top-level keys are ignored. Here is a complete, valid file for a small app.
name: tutorial_app
description: A sample app that fetches a web page.
publish_to: none
version: 1.0.0+1
environment:
sdk: ^3.4.0
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: trueThe table describes each top-level key.
| Key | Purpose |
|---|---|
| `name` | The package name. Use lowercase letters, digits, and underscores. This name appears in `package:tutorial_app/...` imports. |
| `description` | A short summary. Required when publishing, optional for apps. |
| `publish_to` | Set to `none` to stop `pub publish` from uploading the package by accident. |
| `version` | Your own version. In `1.0.0+1`, the part after `+` is the build number, which Flutter passes to Android and iOS. |
| `environment` | The SDK versions your project supports. |
| `dependencies` | Packages needed when the app runs. |
| `dev_dependencies` | Packages needed only while developing and testing. |
| `dependency_overrides` | Forced versions that win over everything else. Use it rarely. |
| `flutter` | Flutter-specific settings such as assets and fonts. |
The name field matters more than it looks. Dart uses it to resolve imports of your own code. If you rename the package, you must update every package:old_name/... import.
How the sections relate
Think of the file as three groups: identity, requirements, and Flutter build settings.
The pub tool uses the first four groups. The flutter: section only affects the Flutter build, so editing it never changes which packages you download.
The environment section
environment declares which Dart SDK your code needs. The pub tool refuses to resolve dependencies if the installed SDK falls outside the range.
environment:
sdk: ^3.4.0This means "Dart 3.4.0 or newer, below 4.0.0". The Dart SDK ships inside Flutter, so each Flutter release implies a Dart version. If you run flutter pub get on an older Flutter and see an error about the SDK constraint, the cause is the SDK range in this section, or in a package you depend on.
Packages carry their own environment ranges. A package that requires a newer Dart than you have cannot be added, even if its API is exactly what you want.
The dependencies section
dependencies is a map from package name to a description of where to get it. The simplest form is a name and a version constraint:
dependencies:
http: ^1.2.0This is a hosted dependency, meaning the package comes from pub.dev. The value ^1.2.0 is a version constraint. The next chapters cover constraints in detail.
Other sources use a nested map instead of a version string:
dependencies:
flutter:
sdk: flutter
my_local_package:
path: ../my_local_package
my_forked_package:
git:
url: https://github.com/example/my_forked_package.git
ref: mainEach entry uses a different source:
sdk: flutterpoints to a package bundled with the Flutter SDK. Theflutterpackage itself is the usual case.path:points to a directory on your machine.git:points to a repository.refcan be a branch, tag, or commit.
A later chapter covers path and git dependencies. For now, recognize that a plain name: version line means pub.dev, and a nested map means another source.
Indentation carries the meaning
YAML uses indentation to show structure, so a small spacing change alters meaning. Compare these two entries:
dependencies:
flutter:
sdk: flutterHere sdk: flutter is nested under flutter. It tells pub that the flutter package comes from the SDK. If sdk: flutter were at the same level as flutter:, YAML would read flutter as having a null value and sdk as a package called "sdk". Pub then reports an error about a missing package or an invalid description.
Keep these rules in mind:
- Use spaces, never tabs. YAML forbids tab indentation.
- Use two spaces per level, and use the same width throughout the file.
- Put one space after each colon.
- Do not repeat a package name within a section. Duplicate keys are an error.
Do not confuse flutter: in two places
The word flutter appears in two roles. Under dependencies and dev_dependencies, flutter: with sdk: flutter is a package entry. At the top level, flutter: is the configuration section:
flutter:
uses-material-design: true
assets:
- assets/images/logo.pngMixing them up is a common mistake. Adding a package under the top-level flutter: section has no effect on dependency resolution. Adding assets: under dependencies produces an error.
dev_dependencies and dependency_overrides
dev_dependencies uses the same syntax as dependencies. The difference is what happens to the package afterward. Dev dependencies are used by tools and tests, such as flutter_test and lint rules, and they are not exposed to apps that depend on your package. A later chapter covers this split.
dependency_overrides also uses the same syntax, but it overrides the constraints that other packages declare. It is a last resort for resolving conflicts, and it can hide real incompatibilities. Treat any entry there as temporary.
Editing the file and applying changes
You can change dependencies in two ways:
- Run
flutter pub add <name>, which edits the file for you and then resolves dependencies. - Edit
pubspec.yamlby hand, then runflutter pub get.
A hand edit changes only the text. Nothing is downloaded until pub runs. Many editors run pub get automatically when you save, but do not rely on that. If the editor shows "Target of URI doesn't exist" after you add a package, the likely cause is that resolution has not run.
Common errors
The table lists the failures you will meet most often while editing the file.
| Symptom | Likely cause | Fix |
|---|---|---|
| `Error on line N: Expected a key while parsing a block mapping` | A tab character, or a line indented under a scalar | Replace tabs with spaces and fix the indentation |
| `Could not find package "x" at https://pub.dev` | Misspelled package name | Check the exact name on pub.dev |
| `A package may not list itself as a dependency` | The `name` field matches a dependency | Rename your project or remove the entry |
| `The current Dart SDK version is X, but package requires Y` | The `environment` range or a package's range excludes your SDK | Upgrade Flutter, or choose a package version that supports your SDK |
| `Because tutorial_app depends on http ^1.2.0 which doesn't match any versions` | The version does not exist | Pick a version that is published |
| Imports fail after editing by hand | `pub get` has not run | Run `flutter pub get` |
The last two errors come from version solving. A later chapter shows how to read those messages in full.
Exercises
Exercise
A teammate adds http: ^1.2.0 to pubspec.yaml but accidentally indents it so it sits under flutter: (the dependency entry) with two extra spaces. Pub now reports an error. Why does the indentation change the result, and what is the correct fix?
Show answer
YAML indentation defines nesting. With the extra indentation, http becomes a child key of the flutter entry instead of a sibling package, so pub reads it as part of the flutter package's description and rejects it. The fix is to align http: ^1.2.0 with the other package names, two spaces under dependencies:, then run flutter pub get.
Exercise
Which section of pubspec.yaml should hold flutter_lints, and which section holds a package such as http that your app calls at runtime? What does the top-level flutter: section do?
Show answer
flutter_lints belongs in dev_dependencies, because it is a development tool. http belongs in dependencies, because the running app uses it. The top-level flutter: section holds Flutter build settings such as uses-material-design and assets. It does not control which packages are downloaded.