Sub topic 6
Resolving Dependencies with `pub get` and `pubspec.lock`
This chapter assumes you know basic Dart syntax, can run terminal commands, and understand YAML. You should also have a Flutter project created with flutter create.
Adding a dependency does not immediately place usable Dart code in your project. Flutter uses Dart's package manager, called pub, to calculate which package versions can work together. The flutter pub get command performs this calculation and downloads the required packages.
What `pub get` does
When you run:
flutter pub getPub reads pubspec.yaml, examines the dependency constraints, and builds a complete dependency graph. A dependency graph shows every package your app needs, including packages required by your direct dependencies.
For example, your app might depend directly on http. The http package might depend on another package, such as meta. Pub resolves both packages and checks that their version requirements do not conflict.
flowchart TD
A[pubspec.yaml] --> B[Read dependency constraints]
B --> C[Find compatible versions]
C --> D[Download packages]
D --> E[Write pubspec.lock]
E --> F[Flutter imports packages]The command can also update generated project files and platform integration data when a plugin is added. A plugin is a package that includes native code for Android, iOS, web, or another platform.
The role of `pubspec.lock`
pubspec.yaml describes acceptable versions. pubspec.lock records the specific versions selected for this project.
| File | Main purpose | Typical contents |
|---|---|---|
| `pubspec.yaml` | Declares project requirements | `http: ^1.2.0` |
| `pubspec.lock` | Records one resolved set | `http: 1.2.2` and its source |
Suppose pubspec.yaml contains:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0The caret constraint ^1.2.0 allows versions from 1.2.0 up to, but not including, 2.0.0. Pub might select 1.2.2 today. That exact selection is written to pubspec.lock.
The lockfile also records transitive dependencies, which are dependencies of your dependencies. This makes installations more repeatable. Another developer or a continuous integration server can run flutter pub get and start with the same resolved versions, as long as those versions remain available.
`pub get` versus `pub upgrade`
flutter pub get tries to preserve versions already recorded in pubspec.lock. It changes the lockfile when a new dependency is added, a constraint changes, or a locked version can no longer be used.
flutter pub upgrade intentionally looks for newer versions that still satisfy the constraints:
flutter pub upgrade
flutter pub outdatedUse pub get for normal setup and after pulling project changes. Use pub upgrade when you deliberately want to refresh dependencies. Review the resulting lockfile and run tests afterward.
Common errors
A version solving failure occurs when no single set of package versions satisfies all constraints. For example, one package might require shared_preferences below 3.0.0, while another requires shared_preferences at least 3.0.0.
Read the error from the bottom upward. It usually names the packages and constraints that disagree. Possible fixes include upgrading one dependency, choosing an older compatible release, or replacing a package. Do not delete the lockfile as a first response. Deleting it can produce a different set of versions without solving the underlying constraint conflict.
Exercise
Your pubspec.yaml allows http versions from 1.2.0 to below 2.0.0, while pubspec.lock records 1.2.2. Which command should you normally run after cloning the project, and which command should you use to intentionally search for newer compatible versions?
Show answer
Run flutter pub get after cloning. Run flutter pub upgrade when you intentionally want to search for newer versions within the declared constraints.