Sub topic 6
pub get and pubspec.lock
This chapter assumes you can edit pubspec.yaml, run flutter pub add, and read caret constraints such as ^1.2.0. It explains what happens after you change a constraint: how the pub tool turns constraints into exact versions, and how pubspec.lock records them.
Constraints versus resolved versions
pubspec.yaml states what you will accept. It says "any http from 1.2.0 up to, but not including, 2.0.0." It does not say which version you get.
The resolved version is the exact one pub picks, such as 1.2.2. This is a separate fact from the constraint, and it lives in pubspec.lock.
| File | Written by | Contains | Meaning |
|---|---|---|---|
| `pubspec.yaml` | You (or `pub add`) | Version ranges | What is allowed |
| `pubspec.lock` | The pub tool | Exact versions | What was chosen |
What `pub get` does
flutter pub get reads pubspec.yaml, finds a set of package versions that satisfies every constraint, downloads those packages, and writes the result to pubspec.lock. It also writes .dart_tool/package_config.json, which tells the Dart compiler where each package's code lives on disk.
flutter pub getThe steps, in order:
- Read your constraints and those of every package you depend on, including indirect (transitive) ones.
- If a
pubspec.lockexists, prefer the versions it already lists. - Solve for a version of every package that satisfies all constraints at once.
- Download any package that is not in the local cache.
- Write
pubspec.lockand.dart_tool/package_config.json.
Step 2 matters most. When a lock file exists, pub get keeps the locked versions wherever they still satisfy your constraints. A newer release of http on pub.dev does not change your project by itself.
A worked example
Start with this pubspec.yaml fragment:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0After flutter pub get, pubspec.lock contains an entry like this. The version numbers are examples and will differ on your machine.
packages:
http:
dependency: "direct main"
description:
name: http
sha256: "b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d5a4b0c9eaf8fd8d7c3"
url: "https://pub.dev"
source: hosted
version: "1.2.2"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
sdks:
dart: ">=3.3.0 <4.0.0"Each entry has these fields:
dependency:direct main(you listed it underdependencies),direct dev(underdev_dependencies),direct overridden, ortransitive(pulled in by another package).description: where it came from. For hosted packages this includes thesha256hash of the archive and the server URL.source:hosted,git,path, orsdk.version: the exact version chosen.
You wrote one dependency. The lock file lists several, because http itself depends on http_parser, async, web, and others.
Why the hash matters
The sha256 value is the checksum of the downloaded archive. On later runs pub compares the archive against it. If someone replaces the contents of a published version, which pub.dev does not normally allow, the mismatch is detected. The lock file therefore pins integrity as well as version.
Locked versions and new releases
Suppose 1.2.2 is locked and 1.3.0 is released. Your constraint ^1.2.0 allows it, but pub get keeps 1.2.2. This is deliberate: every machine that uses the same lock file builds with the same code.
To move to a newer version you must ask for it:
flutter pub upgradepub upgrade ignores the locked versions and picks the newest ones your constraints allow, then rewrites pubspec.lock. The next chapters cover upgrading in more detail. The point here is that pub get and pub upgrade differ in their treatment of the lock file.
| Command | Uses existing lock file? | Typical use |
|---|---|---|
| `flutter pub get` | Yes, keeps locked versions | Sync after cloning or editing `pubspec.yaml` |
| `flutter pub upgrade` | No, picks newest allowed | Deliberately take newer versions |
When `pub get` runs
You rarely need to run it by hand:
flutter createruns it for a new project.flutter pub addandflutter pub removerun it for you.flutter runandflutter buildcheck whetherpubspec.yamlis newer than the lock file and resolve again if needed.- Most IDEs run it when you save
pubspec.yaml.
You do need it after cloning a repository, switching branches, or editing pubspec.yaml outside an IDE.
Should you commit `pubspec.lock`?
The answer depends on what you are building.
- Applications (a Flutter app): commit
pubspec.lock. Teammates and your CI system then build with the same versions you tested. - Libraries and packages published to pub.dev: the Dart team's guidance is not to commit it. Users of your package get their own resolution, and your tests should run against the newest versions your constraints allow.
Flutter's default .gitignore does not exclude pubspec.lock, so it is committed unless you change that.
Reproducible builds in CI
On a build server, you want the exact versions you committed. Run:
flutter pub get --enforce-lockfileWith --enforce-lockfile, the command fails if the lock file is missing, if it does not satisfy pubspec.yaml, or if resolving would need to change it. This catches the case where a developer edited pubspec.yaml but forgot to commit the updated lock file. This flag requires a reasonably recent Dart SDK, so check your version if the option is not recognized.
Common problems
The lock file changes after you only ran pub get. If you edited pubspec.yaml, pub must re-solve, and it updates the lock file. Review the diff and commit it with your pubspec.yaml change.
A teammate sees different versions. The lock file is probably not committed, or is in .gitignore. Check with git ls-files pubspec.lock.
Import errors right after adding a package. The analyzer reads .dart_tool/package_config.json. If an IDE shows Target of URI doesn't exist, run flutter pub get and restart the Dart analysis server.
Deleted lock file gives surprising upgrades. Without the lock file, pub get behaves like a fresh resolution and picks the newest allowed versions. That can pull in a release with a behavior change. Restore the file from version control instead.
Merge conflicts in pubspec.lock. Do not merge the text by hand. Resolve pubspec.yaml first, then delete the conflict markers by taking either side of the lock file and run flutter pub get. Pub rewrites it to match the merged constraints.
Inspecting what was resolved
To see the full resolved tree without opening the lock file, use:
flutter pub depsIt prints each direct dependency and the packages it brings in. This helps when you wonder why a package you never listed is in your build.
Exercise
You run flutter pub get on a project whose pubspec.lock pins http at 1.2.2. The constraint is http: ^1.2.0, and 1.3.0 has just been published. Which version do you get, and which command would move you to 1.3.0?
Show answer
You get 1.2.2, because pub get keeps locked versions that still satisfy the constraint. Run flutter pub upgrade (or flutter pub upgrade http for just that package) to move to 1.3.0, which ^1.2.0 allows.
Exercise
Your CI build must fail if a developer changes pubspec.yaml but forgets to commit the matching pubspec.lock. Which command do you run in CI, and for a published library, would you commit the lock file?
Show answer
Run flutter pub get --enforce-lockfile. For a published library you would normally not commit pubspec.lock, because consumers resolve their own versions. For an application you commit it.