Sub topic 14
Dependency Overrides and When to Avoid Them
This chapter assumes you know basic Dart syntax, can read YAML, and have used flutter pub get in a Flutter project. You should also understand that packages can depend on other packages.
What a dependency override does
Dart's package solver normally chooses versions that satisfy every declared constraint. A dependency override tells the solver to use a particular version or source anyway.
Overrides are written under dependency_overrides in pubspec.yaml:
name: override_example
environment:
sdk: ">=3.3.0 <4.0.0"
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dependency_overrides:
collection: 1.19.0In this example, your project asks for collection version 1.19.0, even if another dependency declares a different compatible constraint. The override applies to the package resolution for this project.
An override does not change the dependency's published constraint. It only changes what your local application uses.
Why overrides can help
An override is useful when you need to test a fix before it is published. For example, a package may have a bug fixed on a Git branch, or two packages may temporarily disagree about a version.
A Git override can test an unreleased revision:
dependency_overrides:
shared_models:
git:
url: https://github.com/example/shared_models.git
ref: fix-json-decodingA path override is useful when you develop two packages together:
dependency_overrides:
shared_models:
path: ../shared_modelsThese approaches let the application use code that is not yet available from pub.dev. They are especially useful during local development, debugging, and short-lived experiments.
Risks and common errors
Overrides bypass compatibility checks. The selected package might not work with the package that requested another version. The project can compile while failing later at runtime or producing incorrect data.
Common risks include:
- A breaking API change is forced into an older package.
- A local path works on one computer but does not exist in continuous integration.
- A Git branch changes or disappears.
- An override hides a real version-solving problem.
- A published application accidentally depends on unreleased code.
- The lockfile records a resolution that other developers cannot reproduce.
An override also affects the whole dependency graph. It is not limited to one import or one feature.
> callout warning > Treat an override as a temporary exception. Add a comment explaining why it exists, what issue it addresses, and when it should be removed.
A safer workflow
Use this sequence when a normal dependency declaration does not work:
- Read the solver error and identify the conflicting constraints.
- Check whether newer package versions resolve the conflict.
- Upgrade or replace the incompatible package when practical.
- Use an override only to test a known compatible version or unreleased fix.
- Run unit tests, integration tests, and platform builds.
- Record the reason in version control.
- Remove the override as soon as the normal package releases a fix.
Do not use an override merely to silence an error. A successful resolution is not proof that the packages are compatible.
Check the resolved graph with:
flutter pub deps
flutter pub outdatedKeep overrides in application projects when possible. A reusable package should usually declare honest dependency constraints instead of forcing its consumers to use one version.
Exercise
A package requires collection: ^1.18.0, but your application overrides it with collection: 2.0.0. What should you do before keeping the override?
Show answer
Check the package's compatibility with version 2.0.0, run the relevant tests and builds, document the reason, and look for a normal dependency upgrade or published fix.