Sub topic 14
Dependency overrides and when to avoid them
This chapter assumes you can read pubspec.yaml, understand caret constraints such as ^1.2.0, and know that pubspec.lock records the resolved versions. It also assumes you have seen a version solving failure. The chapter covers what dependency_overrides does, how to scope it, what can go wrong, and what to try first.
What an override does
Normally the pub solver picks one version of each package that satisfies every constraint in the dependency graph. Your app asks for http: ^1.2.0. Each package you depend on may ask for http too, with its own range. If the ranges have no overlap, resolution fails.
A dependency override tells the solver to use the version or source you name and to ignore every other constraint on that package. The ranges declared by your direct and transitive dependencies are no longer checked for that package.
dependencies:
flutter:
sdk: flutter
package_a: ^1.0.0
package_b: ^2.0.0
dependency_overrides:
intl: 0.19.0Suppose package_a requires intl ^0.18.0 and package_b requires intl ^0.19.0. Without the override, flutter pub get fails. With it, pub resolves intl to 0.19.0 and package_a is built against a version its author never declared support for.
The solver is silenced, not satisfied
The override does not make the packages compatible. It only removes the check. Whether package_a still works with intl 0.19.0 is a question about code, not about version numbers.
An override can lead to three kinds of outcome:
- The packages happen to be compatible. The author's upper bound was conservative, and everything works.
- The break shows up at compile time, such as a missing class or a changed method signature. This is the easiest case to notice.
- The break shows up at runtime, such as a
NoSuchMethodErroron one screen or a changed default behavior. This is the worst case, because analysis and a quick launch may both pass.
pub prints a warning on every resolution while overrides exist. Read it as a reminder that you are responsible for compatibility now.
Warning: You are using these overridden dependencies:
! intl 0.19.0Where overrides apply
Only the root package's overrides count. If you depend on a package that has its own dependency_overrides, pub ignores them. Overrides are a local, per-build decision, not something that travels with a package.
This has two consequences:
- Publishing a package with an override does not protect its users. They get the constraints you declared, and the conflict you hid comes back for them.
- A fix that works in your app does not carry over to other apps or to other developers' packages.
Overrides can change the source
An override can use any dependency source, not only a version. You can point at a path or a git ref, as in the earlier chapter on git and path dependencies.
dependency_overrides:
package_a:
git:
url: https://github.com/example/package_a.git
ref: fix-intl-bounds
shared_models:
path: ../shared_modelsThis is the legitimate use case. You have a fork of package_a with the constraint relaxed. Overriding package_a with the fork means that every package that depends on it, directly or indirectly, gets the fork. That is something a normal dependency entry cannot do, because a normal entry still has to agree with what other packages demand.
Keeping overrides out of the shared pubspec
Dart supports a second file, pubspec.overrides.yaml, next to pubspec.yaml. It holds only the overrides, and pub merges it with the main file.
# pubspec.overrides.yaml
dependency_overrides:
shared_models:
path: ../shared_modelsThis file suits local experiments, such as testing a sibling package you are editing. Teams usually add it to .gitignore, so the committed pubspec.yaml stays honest and each developer decides what to override on their machine. Pub also ignores this file when you run pub publish.
You can add an override from the command line too. The override: prefix works like dev: does in pub add.
flutter pub add override:intl:0.19.0To remove the override, delete the entry and run flutter pub get. You can also use flutter pub remove override:intl.
How an override interacts with the lockfile
The override affects resolution, so pubspec.lock records the chosen version as usual. Nothing in the lock marks a package as overridden. A teammate who opens the project sees the version in the lock and the entry in pubspec.yaml, but the file gives no hint that this version breaks someone's declared constraint.
Because of that, treat each override as technical debt that needs a written reason. Add a comment with the date, the conflict, and the condition for removal.
dependency_overrides:
# 2026-11: package_a 1.4.0 caps intl at <0.19.0 but works with 0.19.0.
# Remove when package_a >= 1.5.0 is released (issue #123).
intl: 0.19.0Decision flow before you override
Try the options on the left side of this diagram first. They fix the cause. An override treats the symptom.
When an override is reasonable
Overrides are a sound tool in a few specific cases:
- A transitive dependency has an upper bound that is too strict. You tested the newer version, and its maintainer has not released a fix yet.
- You are developing several local packages together and need your app to use the local copy of one of them everywhere in the graph.
- You need to pin a security fix in a transitive package before the intermediate packages update.
- You are debugging. You use a temporary override to check whether a particular version is the cause of a problem.
In each case the override is a bridge that is expected to go away.
When to avoid an override
Avoid an override in these situations:
- You have not read the solver message. The conflict may name a package you can simply upgrade.
- The conflict involves a package with a native part (a plugin). Mismatched Android, iOS, or web code fails at build time with errors that are hard to trace back to a version number.
- The conflicting package is the Flutter SDK or a package pinned to it, such as
flutter_test. Overriding these is almost always wrong. Change the Flutter version or the dependency instead. - You are writing a library. Overrides are ignored for your consumers, so you would ship a conflict you cannot see.
- A major version differs, for example
^1.0.0against^2.0.0. Major bumps signal breaking API changes, and the override just defers the error.
Testing an override
If you add one, check that it works before you commit it:
- Run
flutter pub getand confirm the warning lists only the packages you intended. - Run
flutter analyzeto catch compile-time API breaks. - Run your test suite, including any tests that touch the overridden package.
- Exercise the features that use the packages concerned on each platform you ship to.
- Run
flutter pub outdatedperiodically. Check whether the blocker has released a version that makes the override unnecessary.
You can also use flutter pub deps to see which package pulls in the overridden one, so you know which code paths to test.
Common mistakes
| Mistake | Result | Better approach |
|---|---|---|
| Override added to silence an error without reading it | Hidden break at runtime | Read the conflict and try an upgrade first |
| No comment on the override | Nobody knows when it is safe to remove | Record the reason and the removal condition |
| Override left in after the blocker is fixed | Stale pin hides future upgrades | Review overrides when running `pub outdated` |
| Overrides in a published package | Consumers still hit the conflict | Fix constraints in the package itself |
| Overriding to a version outside what the code supports | Compile or runtime failure | Test the exact version before committing |
Exercise
Your app depends on package_a ^1.0.0, which requires intl >=0.18.0 <0.19.0, and on package_b ^2.0.0, which requires intl ^0.19.0. flutter pub get fails. List the steps you take before adding intl: 0.19.0 under dependency_overrides, and say what you do if you decide to add it.
Show answer
First read the solver message to confirm which packages conflict. Run flutter pub outdated and flutter pub upgrade to see whether a newer package_a allows intl 0.19. If not, check whether package_a can be replaced or removed, and open an issue or try a git fork with a relaxed bound. If you still add the override, write a comment with the reason and the condition for removal, run flutter analyze and the tests, and manually check the features that use package_a on each platform. Prefer pubspec.overrides.yaml if the override is only for your machine.
Exercise
A package you publish to pub.dev contains dependency_overrides that resolves a conflict between two of its dependencies. Does the override help the people who install your package? Why or why not?
Show answer
No. Pub only applies the overrides of the root package, which is the app being built. Anyone who depends on your package ignores your overrides and sees the original conflicting constraints, so the conflict reappears for them.