Sub topic 2
Where dependencies live: pubspec.yaml
This chapter assumes you have basic Dart syntax, a Flutter project created with flutter create, terminal experience, and basic YAML knowledge. You will learn how pubspec.yaml describes the packages your app needs.
The role of pubspec.yaml
pubspec.yaml is the project manifest. A manifest is a file that records a project's metadata, SDK requirements, assets, and dependencies. Flutter and Dart tools read this file when they resolve packages.
A new Flutter project usually contains sections like these:
name: sample_app
description: A Flutter application.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ^3.4.0
flutter: '>=3.22.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: trueThe top-level keys have different purposes:
| Key | Purpose |
|---|---|
| `name` | The package or application name |
| `environment` | Supported Dart and Flutter SDK versions |
| `dependencies` | Packages needed when the application runs |
| `dev_dependencies` | Packages needed for tests, analysis, or code generation |
| `flutter` | Flutter-specific settings such as assets and fonts |
YAML uses indentation to show structure. Use spaces, not tabs. The child value must be indented consistently under its parent.
The dependencies section
A dependency is another package that your project imports or uses. Runtime dependencies belong under dependencies.
The Flutter SDK is itself represented as a dependency:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0This entry means that the project uses the flutter package supplied by the installed Flutter SDK. The http entry refers to a hosted package, normally downloaded from pub.dev.
A package name is followed by a version constraint. The constraint tells the package solver which releases are acceptable. The expression ^1.2.0 accepts compatible 1.x releases beginning at 1.2.0, but it does not accept 2.0.0.
Keep each package at the same indentation level:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
provider: ^6.1.2A package can also have its own configuration. That configuration is nested farther to the right:
dependencies:
my_local_package:
path: ../my_local_packageThe path value points to a package directory relative to the current project's directory.
Runtime and development dependencies
Use dependencies for packages required by the application at runtime. Examples include HTTP clients, state-management libraries, and database clients.
Use dev_dependencies for tools used only while developing or testing. Examples include test packages, linters, and code generators.
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.4.11Putting a runtime package in dev_dependencies can make the project declaration misleading and can cause packaging or deployment problems. Putting a tool in dependencies adds unnecessary runtime baggage.
Common YAML errors
These mistakes often prevent dependency resolution:
- Using a tab instead of spaces.
- Misspelling a package name.
- Placing a package under the wrong section.
- Indenting
sdkat the same level asflutterinstead of under it. - Writing an unquoted value containing YAML punctuation.
- Declaring the same package in both dependency sections.
After editing the file, run:
flutter pub getThis validates the manifest and resolves the declared packages. If the command reports a formatting or version error, read the first reported package and constraint before changing unrelated entries.
Exercise
Where should an HTTP client used by the running app be declared, and where should a test runner be declared?
Show answer
Declare the HTTP client under dependencies and the test runner under dev_dependencies.