Sub topic 12
Understanding version solving failures
This chapter assumes you can run flutter pub add and flutter pub get, read a caret constraint such as ^1.2.0, and know that pubspec.lock records the resolved versions. By the end you should be able to read a failure message, identify which constraints collide, and pick the smallest fix.
What version solving does
When you run flutter pub get, flutter pub add, or flutter pub upgrade, the pub tool has to find one version of every package in your dependency graph. The graph includes your direct dependencies and everything they depend on. The chosen set must satisfy every constraint at once. This is called version solving.
Pub allows only one version of a package in an app. If foo needs collection ^1.18.0 and bar needs collection ^2.0.0, no single version of collection satisfies both. Solving then fails, and pub prints an explanation.
Constraints come from four places:
- Your own
pubspec.yaml, independenciesanddev_dependencies. - The
pubspec.yamlof each package you depend on, and of the packages those depend on. - The Dart SDK and Flutter SDK versions installed on your machine, checked against each package's
environmentsection. - Packages pinned by the Flutter SDK itself.
flutter_testandflutterbring fixed versions ofmeta,collection,characters,material_color_utilities, and a few others.
Pub tries newer versions first. If it hits a conflict, it backtracks and tries older versions of the packages involved. It reports failure only after it has exhausted the combinations. So a failure means no valid combination exists for your constraints. It does not mean pub gave up early.
Anatomy of a failure message
Pub's error is a chain of "Because ... and ..., ..." sentences that ends with "version solving failed". This is an illustrative message with made-up package names:
Resolving dependencies...
Because photo_grid >=3.0.0 depends on collection ^1.19.0 and every
version of flutter_test from sdk depends on collection 1.18.0,
photo_grid >=3.0.0 is incompatible with flutter_test from sdk.
So, because my_app depends on both flutter_test from sdk and
photo_grid ^3.0.0, version solving failed.Read it from the bottom up:
- The last line names the root cause: your app asked for two things that cannot coexist.
- The middle lines show the constraint each side imposes. Here,
photo_grid3.x wantscollection ^1.19.0, but the Flutter SDK pinscollectionto 1.18.0. - The top lines show how each requirement was derived. Every step is a statement pub proved while backtracking.
Look for the phrases "from sdk", "from git", and "from path". They tell you the source of a constraint, which affects what you can change. You cannot edit a constraint that comes from the SDK. You can only choose a different SDK version or a different version of the package.
Common causes
| Cause | Typical message fragment | Usual fix |
|---|---|---|
| Two packages need incompatible ranges of a shared dependency | `X depends on Y ^1.0.0 and Z depends on Y ^2.0.0` | Upgrade or downgrade one of X or Z so their ranges overlap |
| The package needs a newer SDK than you have | `requires SDK version >=3.5.0 <4.0.0` | Upgrade Flutter, or pick an older package version |
| The Flutter SDK pins a transitive package | `every version of flutter_test from sdk depends on ...` | Use a package version compatible with your Flutter release |
| Your own constraint is too tight | `my_app depends on foo ^1.0.0 which doesn't match any versions` | Loosen the constraint in `pubspec.yaml` |
| Wrong name or unpublished version | `Could not find package "fooo"` or `doesn't match any versions` | Check spelling on pub.dev, check the version exists |
| Git or path package declares a conflicting range | `foo from git depends on ...` | Fix the constraint in the forked package |
A diagnosis workflow
Step 1: Find the shared dependency
Identify the package that appears in two "depends on" clauses with different ranges. In the example, it is collection. This is the contested package.
Step 2: See what the tree looks like
If your project resolved before you made the last change, inspect the graph that worked:
flutter pub deps --style=compactThis prints each package and its dependencies. Undo your most recent change, or run it from a clean checkout, to see which package pulls in the contested one and at what version.
Step 3: Check which versions exist
flutter pub outdatedThe output has four columns: Current, Upgradable, Resolvable, and Latest. Read them like this:
- If Upgradable differs from Latest, your own constraint holds the package back.
- If Resolvable differs from Latest, another package in the graph holds it back.
- If a package has no Resolvable version, it conflicts with something you must change first.
The package's Versions tab on pub.dev lists each release and the SDK range it supports. Its Changelog shows when it raised the minimum for shared dependencies.
Step 4: Move one side
You have three real options:
- Upgrade the package whose constraint is too old. Use
flutter pub upgrade --major-versions footo raise the constraint inpubspec.yamlto the latest major version, or edit the line by hand. - Downgrade the package that demands too much. For example,
flutter pub add photo_grid:^2.4.0picks the last release that works with your SDK pins. - Replace one package with an alternative that has fewer or looser dependencies.
Change one thing at a time and re-run flutter pub get. If you change three constraints together, a success tells you nothing about which change mattered.
SDK constraint failures
Every published package declares an environment section:
environment:
sdk: ">=3.4.0 <4.0.0"
flutter: ">=3.22.0"Your app has the same section. If a dependency requires a Dart version above the one in your installed Flutter, you see a message like this:
The current Dart SDK version is 3.3.0.
Because my_app depends on photo_grid >=3.0.0 which requires SDK
version >=3.5.0 <4.0.0, version solving failed.You have two ways out. Upgrade Flutter with flutter upgrade, which brings a newer Dart SDK, or pin the package to an older release that supports your SDK. Pub's backtracking often does the second for you when your constraint is a caret range. It selects the newest release that fits. If it still fails, no release of that package in your range fits.
Also check your own environment section. If your app says sdk: ">=3.0.0 <4.0.0" and the package needs >=3.5.0, the lower bound in your file does not matter. The installed SDK is what counts.
Why you should treat the lockfile carefully
A conflict can appear after you pull a teammate's commit that changed pubspec.yaml but not pubspec.lock, or the reverse. Always commit both files for an app. Then everyone resolves against the same pinned versions, and a solving failure points to the change you just made rather than to drift.
If the lockfile itself seems wrong, you can delete it and let pub rebuild it. Do this only after committing your work, because the rebuilt lockfile may select newer versions than before.
Dependency overrides: the last resort
When no combination of released versions works, you can force a version in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
photo_grid: ^3.0.0
dependency_overrides:
collection: 1.19.0An override makes pub ignore every other package's constraints on collection and use the version you name. It silences the conflict but does not resolve it. If photo_grid really calls an API that exists only in collection 1.19.0 and the Flutter SDK's code expects 1.18.0 behavior, you may get a compile error or a runtime failure.
Keep these rules in mind:
- Overrides apply only in the root package. Anyone who depends on your package does not inherit them.
- Pub does not warn you about runtime incompatibility. Your tests have to find it.
- Add a comment with the reason and the condition for removal, for example a link to the upstream issue.
- Remove the override as soon as a compatible release exists.
Quick checklist
- Read the last line first, then work upward.
- Name the contested package and the two ranges that do not overlap.
- Run
flutter pub outdatedand look at the Resolvable column. - Change one constraint at a time.
- Commit
pubspec.yamlandpubspec.locktogether. - Use
dependency_overridesonly with a written reason and an expiry condition.
Exercise
Pub reports: "Because myapp depends on both fluttertest from sdk and photogrid ^3.0.0, version solving failed", after explaining that photogrid 3.x needs collection ^1.19.0 while fluttertest from sdk needs collection 1.18.0. Name two fixes that do not use dependencyoverrides.
Show answer
Move to a release of photogrid that accepts collection 1.18.0 (for example `flutter pub add photogrid:^2.4.0`), or upgrade Flutter so that fluttertest from sdk pins a newer collection version that photogrid 3.x accepts. Replacing photo_grid with another package is also valid.
Exercise
After running flutter pub outdated, package foo shows Upgradable 1.4.0, Resolvable 1.4.0, and Latest 2.1.0. What holds it back, and what command changes that?
Show answer
Your own constraint in pubspec.yaml (for example ^1.4.0) excludes 2.x, because Upgradable equals Resolvable and both are below Latest. Run flutter pub upgrade --major-versions foo, or edit the constraint to ^2.1.0 and run flutter pub get. If that then fails, another package limits foo to 1.x.
Exercise
Why is it risky to keep a dependency_overrides entry in a project for months?
Show answer
The override hides a real incompatibility. Pub no longer checks the constraints of other packages on that dependency, so a bug may appear only at compile time or runtime. The override applies only to your root package. It also keeps you from noticing when a compatible release makes the override unnecessary.