Sub topic 12
Understanding Version Solving Failures
This chapter assumes basic Dart syntax, an existing Flutter project, terminal use, and basic YAML. You should also know that pubspec.yaml declares dependencies and pubspec.lock records the versions selected for the project.
What version solving does
When you run flutter pub get, Dart's package solver reads every dependency declaration. It then chooses one compatible version for each package, including transitive dependencies. A transitive dependency is a package required by another package.
For example, your app might depend on http, while another package depends on collection. The solver must satisfy both your direct constraints and all constraints inside the dependency graph.
A failure means that no single set of versions satisfies all constraints. It does not necessarily mean the newest package is broken.
Reading the error
A typical error identifies incompatible constraints:
Because app depends on package_a ^2.0.0 which depends on shared ^1.4.0
and app depends on package_b ^3.0.0 which depends on shared ^2.0.0,
version solving failed.Read the message from the bottom upward:
appis your project.package_aandpackage_bare direct or transitive dependencies.sharedis the package with incompatible constraints.^1.4.0and^2.0.0do not overlap, so the solver cannot choose one version.
A caret constraint usually allows compatible releases below the next major version. Therefore, ^1.4.0 permits versions from 1.4.0 up to, but not including, 2.0.0. It cannot select 2.0.0.
A practical diagnosis
First, reproduce the failure without changing files:
flutter pub getThen inspect the constraints declared by your project:
dependencies:
package_a: ^2.0.0
package_b: ^3.0.0Check available releases and dependency information on pub.dev. You can also ask Pub which packages have newer compatible versions:
flutter pub outdatedUse the output to find whether an upgrade or downgrade can make the constraints overlap.
| Cause | What to look for | Usual fix |
|---|---|---|
| Incompatible transitive constraints | Two packages require different major versions | Upgrade or replace one direct dependency |
| Narrow direct constraint | Your declared range excludes a compatible release | Widen or update the constraint |
| Old lockfile selection | The lockfile keeps an older allowed version | Run `flutter pub upgrade` |
| SDK incompatibility | A package requires a different Dart or Flutter SDK | Upgrade Flutter or choose an older package |
Choosing a fix
Prefer changing direct dependencies rather than editing pubspec.lock manually. The lockfile is generated output. flutter pub upgrade resolves packages again within the constraints in pubspec.yaml and updates the lockfile.
If a package has a newer release that supports the same transitive dependency as another package, update it:
flutter pub add package_a:^2.1.0
flutter pub getIf no compatible release exists, choose one package's older version, replace it, or wait for an upstream fix. Check the package changelog before changing major versions because APIs may have changed.
Overrides and common mistakes
An override forces Pub to use a specified version:
dependency_overrides:
shared: ^2.0.0This can help test a local fix or verify that an upstream constraint is unnecessarily strict. It does not prove that the dependent packages work with that version. Remove the override before publishing or committing a permanent solution unless the project explicitly documents it.
Common mistakes include:
- Treating
pubspec.lockas the source of dependency policy. - Assuming every newer version is compatible with your SDK.
- Changing several constraints at once, making the cause difficult to identify.
- Ignoring the first package relationship named in the solver error.
- Keeping an override after the underlying package releases a fix.
Exercise
Package A requires shared ^1.5.0, and package B requires shared ^2.0.0. Can Pub select shared 2.1.0 without changing either package?
Show answer
No. shared 2.1.0 does not satisfy ^1.5.0. Upgrade, downgrade, replace, or otherwise change one of the constraints.