Sub topic 5
Reading Version Constraints
This chapter assumes you know basic Dart syntax, YAML, and how to run Flutter commands in a terminal. You should also have a Flutter project and understand that packages are listed in pubspec.yaml.
A version constraint tells Dart’s package solver which versions of a dependency are acceptable. The solver combines your constraints with the constraints of every transitive dependency, which is a dependency used by another package.
Exact versions and ranges
A dependency can use an exact version:
dependencies:
http: 1.2.0This accepts only version 1.2.0. Exact versions are predictable, but they can make upgrades difficult because the project cannot use compatible releases automatically.
A range gives the solver more choices:
| Constraint | Meaning |
|---|---|
| `1.2.0` | Only version `1.2.0` |
| `>=1.2.0 <2.0.0` | At least `1.2.0`, but below `2.0.0` |
| `^1.2.0` | Compatible with `1.2.0`, below `2.0.0` |
| `any` | Any available version |
The caret operator (^) is the usual choice for application dependencies. For ^1.2.0, the allowed range is >=1.2.0 <2.0.0. This permits bug fixes and new features within version 1, but it excludes version 2, where breaking changes may exist.
dependencies:
flutter:
sdk: flutter
http: ^1.2.0For versions before 1.0.0, caret behavior is narrower because early releases often make breaking changes in minor versions. For example, ^0.3.2 generally allows versions from 0.3.2 up to, but not including, 0.4.0.
Constraints and pubspec.lock
When you run flutter pub get, Pub resolves a compatible version for each dependency. For an application, the selected versions are recorded in pubspec.lock.
The relationship looks like this:
Suppose pubspec.yaml contains http: ^1.2.0. Pub might select 1.2.2 today and record it in the lockfile. Running pub get again usually keeps that locked version if it still satisfies the constraint. This makes application builds more repeatable.
The constraint remains the project’s policy. The lockfile records the current choice. If you want Pub to reconsider compatible versions, run:
flutter pub upgrade httpTo update all dependencies within their existing constraints, run:
flutter pub upgradeNeither command should be treated as permission to cross a major-version boundary. To adopt a new major release, change the constraint in pubspec.yaml, review the package’s migration notes, and test the application.
Common mistakes
- Using
1.2.0when you intended to receive compatible bug fixes. - Assuming
^1.2.0allows version2.0.0. It does not. - Editing
pubspec.lockby hand instead of changing the constraint and running Pub. - Widening a constraint to solve a conflict without checking API compatibility.
- Forgetting that another package can impose a narrower constraint.
When version solving fails, read the package names and required ranges in the error. First check whether your direct dependency has a stale or unnecessarily narrow constraint. Then check for a newer compatible release of the conflicting package. Avoid dependency_overrides as a permanent solution because it can force incompatible packages together.
Exercise
Your project declares some_package: ^2.1.0. Does Pub consider version 2.9.0 and version 3.0.0 valid?
Show answer
Version 2.9.0 is valid because it is below 3.0.0. Version 3.0.0 is not valid because the caret constraint stops before the next major version.