Sub topic 5
Reading version constraints
This chapter assumes you can edit pubspec.yaml, have run flutter pub add once, and know that versions look like 1.4.2. It explains how to read each constraint in the dependencies section and how the pub tool uses it.
Why constraints exist
A version constraint is the set of versions of a package that your project agrees to work with. Pub chooses one concrete version from that set. Constraints are a promise about compatibility, so they should be as wide as is safe and no wider.
Hosted packages on pub.dev follow semantic versioning, which means a version has the form MAJOR.MINOR.PATCH:
- PATCH changes fix bugs and should not alter the API.
- MINOR changes add features and should stay backward compatible.
- MAJOR changes may break existing code.
Constraints rely on this convention. If a package author breaks it, a constraint cannot protect you.
The caret form
flutter pub add http writes a caret constraint for the latest version it resolved:
dependencies:
http: ^1.2.0^1.2.0 means "1.2.0 or newer, but not 2.0.0". It is shorthand for >=1.2.0 <2.0.0. Versions 1.2.0, 1.2.5, 1.9.3 and 1.99.0 all satisfy it. Versions 1.1.9 and 2.0.0 do not.
The lower bound is the oldest version you know works. Pub never picks anything below it. The upper bound is the next major version, where breaking changes are allowed.
Carets below 1.0.0
Semantic versioning treats 0.x versions as unstable, so the caret moves its upper bound to the next breaking position:
| Constraint | Equivalent range | Allows |
|---|---|---|
| `^1.2.3` | `>=1.2.3 <2.0.0` | `1.2.3` to `1.x.y` |
| `^0.5.2` | `>=0.5.2 <0.6.0` | `0.5.2` to `0.5.y` |
| `^0.0.3` | `>=0.0.3 <0.0.4` | only `0.0.3` |
A package at 0.5.2 therefore gets much less room than one at 1.5.2. This is a common surprise when a young package blocks an upgrade. A minor bump from 0.5.x to 0.6.0 is treated as potentially breaking.
Other constraint forms
You can write ranges and exact versions by hand:
dependencies:
# Caret: the usual choice
http: ^1.2.0
# Explicit range. Quote it, because the value contains spaces and operators.
path: ">=1.8.0 <2.0.0"
# Exact version. Avoid unless you have a specific reason.
collection: 1.18.0
# Lower bound only
meta: ">=1.9.0"
# Any version at all. Discouraged.
characters: any| Form | Meaning | When to use |
|---|---|---|
| `^1.2.0` | `>=1.2.0 <2.0.0` | Default for application and package code |
| `">=1.2.0 <1.5.0"` | Custom range | Skipping a known-bad release or capping a risky one |
| `1.2.0` | Exactly this version | Rare. It makes conflicts with other packages likely |
| `">=1.2.0"` | No upper bound | Avoid. A future major release may break you |
| `any` | No restriction | Avoid. Pub warns about it when publishing |
You must quote any value that contains >, <, = or spaces. Without quotes, YAML may misread it or fail to parse it. A caret value such as ^1.2.0 does not need quotes.
Choosing a constraint on the command line
flutter pub add accepts a constraint after a colon:
flutter pub add http:^1.2.0
flutter pub add "path:>=1.8.0 <2.0.0"Without a constraint, pub picks the newest version compatible with your SDK and your other dependencies, and writes a caret for it.
Testing a constraint yourself
The pub_semver package is the same library that pub uses to parse constraints. This script lets you check a version against a constraint without guessing:
dart pub add pub_semverimport 'package:pub_semver/pub_semver.dart';
void main() {
final constraints = ['^1.2.0', '^0.5.2', '>=1.2.0 <1.5.0'];
final versions = ['1.1.9', '1.2.0', '1.9.3', '2.0.0', '0.5.9', '0.6.0'];
for (final text in constraints) {
final constraint = VersionConstraint.parse(text);
print('Constraint $text');
for (final v in versions) {
final allowed = constraint.allows(Version.parse(v));
print(' $v -> ${allowed ? "allowed" : "rejected"}');
}
}
}Save it as bin/constraints.dart and run dart run bin/constraints.dart. Change the lists and see which versions pass.
How pub applies the constraint
Pub does not install your constraint. It treats every constraint in the project, and in every dependency of those packages, as a rule. Then it searches for one version of each package that satisfies all the rules at once. This process is called version solving.
Three consequences follow:
- Pub prefers the newest allowed version, so a fresh solve on a new machine can land on
1.9.3even though you wrote^1.2.0. - Your constraint is only one of several rules. Another package may require a narrower range of the same dependency, and pub must pick inside the overlap.
- If the rules cannot all be met, resolution fails. A later chapter covers reading those errors.
The version that pub actually chose is recorded in pubspec.lock. The constraint says what is acceptable. The lockfile says what was selected. Changing a constraint does not change the lockfile until pub runs again.
SDK constraints
Constraints also appear in the environment section, where they limit which Dart and Flutter versions your project supports:
environment:
sdk: ^3.5.0
flutter: ">=3.24.0"Pub checks these first. A dependency whose own SDK constraint excludes your installed Dart version is not a candidate at all. This is a frequent reason an older package "does not exist" for you, or a newer one cannot be used until you upgrade Flutter.
Pre-release versions
Versions such as 2.0.0-beta.1 are pre-releases. A constraint does not allow a pre-release unless the constraint itself names one. For example, ^1.2.0 will not select 1.3.0-dev.1, and <2.0.0 will not select 2.0.0-dev.1. To try a pre-release, write it out explicitly:
dependencies:
some_package: ^2.0.0-beta.1This allows 2.0.0-beta.1, later betas, and the final 2.x releases, up to but not including 3.0.0.
Common mistakes
- Using
anyto make an error disappear. It hides the real incompatibility and allows a future major release to break your build. - Pinning an exact version everywhere. If two packages each pin different exact versions of a shared dependency, no solution exists.
- Raising a lower bound without reason. A constraint like
^1.9.0rules out users on1.8.xeven if you never use the new features. Raise the lower bound only when you rely on something added in that release. - Forgetting quotes on a range, which produces a YAML parse error or a wrong value.
- Assuming
^0.x.ybehaves like^1.x.y. It permits only patch updates.
Summary of rules
^X.Y.Zallows everything fromX.Y.Zup to the next major version, or the next minor version whenXis 0.- Ranges with operators must be quoted in YAML.
- Pub picks the newest version that satisfies every constraint in the dependency graph, together with the SDK constraints.
- Constraints describe what is allowed.
pubspec.lockrecords what was chosen.
Exercise
Write the equivalent range for ^0.3.4, and say whether version 0.4.0 is allowed.
Show answer
^0.3.4 equals >=0.3.4 <0.4.0. Version 0.4.0 is not allowed, because below 1.0.0 the caret treats a minor increase as a breaking change.
Exercise
Your pubspec contains path: >=1.8.0 <2.0.0 without quotes, and pub reports a parse problem. What do you change, and why is ^1.8.0 not subject to the same problem?
Show answer
Wrap the value in quotes: path: ">=1.8.0 <2.0.0". The operators and the space make the unquoted value ambiguous for YAML. ^1.8.0 contains no spaces or comparison operators, so it parses as a plain string.
Exercise
You wrote some_package: ^1.2.0. The newest release is 1.7.1, and your colleague's machine has 1.4.0 in pubspec.lock. Which version does each machine use after flutter pub get, and what changes that?
Show answer
Both machines use the version in their pubspec.lock, which is 1.4.0 for your colleague. A fresh checkout with the committed lockfile also gets 1.4.0. Without a lockfile, pub would choose 1.7.1. Running flutter pub upgrade moves the lockfile to the newest version that the constraint allows, which is 1.7.1.