- Dependency sources
- Version constraints
- Dev dependencies
- Dependency overrides
- Best practices
Dependencies are one of the core concepts of the pub package manager. A dependency is another package that your package needs to work. Dependencies are specified in your pubspec. You list only immediate dependencies: the software that your package uses directly. Pub handles transitive dependencies for you.
This page has detailed information on how to specify dependencies. At the end is a list of best practices for package dependencies.
For each dependency, you specify the name of the package you depend on and the range of versions of that package that you allow. You can also specify the source. The source tells pub how to locate the package.
As an example, you specify a dependency in the following format:
This YAML code creates a dependency on the
using the default package repository (pub.dev) and
allowing any version from
2.0.0 (but not including
To learn about this syntax, check out
To specify a source other than pub.dev,
For example, the following YAML code uses
to tell pub to get
transmogrify from a local directory:
The next section describes the format for each dependency source.
Pub can use the following sources to locate packages:
A hosted package is one that can be downloaded from the pub.dev site (or another HTTP server that speaks the same API). Here’s an example of declaring a dependency on a hosted package:
This example specifies that your package depends on a hosted package named
transmogrify and works with any version from 1.4.0 to 2.0.0
(but not 2.0.0 itself).
If you want to use your own package repository,
you can use
hosted to specify its URL.
The following YAML code creates a dependency on the
sdk: '>=2.15.0 < 3.0.0'
The version constraint is optional but recommended. If no version constraint is
any is assumed.
Sometimes you live on the bleeding edge and need to use packages that haven’t been formally released yet. Maybe your package itself is still in development and is using other packages that are being developed at the same time. To make that easier, you can depend directly on a package stored in a Git repository.
git here says this package is found using Git, and the URL after that is
the Git URL that can be used to clone the package.
Even if the package repo is private, if you can connect to the repo using SSH, then you can depend on the package by using the repo’s SSH URL:
If you want to depend on a specific commit, branch, or tag,
ref key to the description:
The ref can be anything that Git allows to identify a commit.
Pub assumes that the package is in the root of the Git repository. To specify a
different location in the repo, specify a
path relative to the repository
The path is relative to the Git repo’s root.
Git dependencies are not allowed as dependencies for packages uploaded to pub.dev.
Sometimes you find yourself working on multiple related packages at the same time. Maybe you are creating a framework while building an app that uses it. In those cases, during development you really want to depend on the live version of that package on your local file system. That way changes in one package are instantly picked up by the one that depends on it.
To handle that, pub supports path dependencies.
This says the root directory for
For this dependency, pub generates a symlink directly to the
of the referenced package directory. Any changes you make to the dependent
package are seen immediately. You don’t need to run pub every time you
change the dependent package.
Relative paths are allowed and are considered relative to the directory containing your pubspec.
Path dependencies are useful for local development, but do not work when sharing code with the outside world—not everyone can get to your file system. Because of this, you cannot upload a package to the pub.dev site if it has any path dependencies in its pubspec.
Instead, the typical workflow is:
- Edit your pubspec locally to use a path dependency.
- Work on the main package and the package it depends on.
- Once they’re both working, publish the dependent package.
- Change your pubspec to point to the now hosted version of its dependent.
- Publish your main package too, if you want.
The SDK source is used for any SDKs that are shipped along with packages, which may themselves be dependencies. Currently, Flutter is the only SDK that is supported.
The syntax looks like this:
The identifier after
sdk: indicates which SDK the package comes from.
flutter, the dependency is satisfiable as long as:
- Pub is running in the context of the
- The Flutter SDK contains a package with the given name
If it’s an unknown identifier, the dependency is always considered unsatisfied.
Let’s say that your Package A depends upon Package B. How can you communicate to other developers which version of Package B remains compatible with a given version of Package A?
To let developers know version compatibility, specify version constraints. You want to allow the widest range of versions possible to give your package users flexibility. The range should exclude versions that don’t work or haven’t been tested.
The Dart community uses semantic versioning1.
You can express version constraints using either traditional syntax or caret syntax. Both syntaxes specify a range of compatible versions.
The traditional syntax provides an explicit range like
The caret syntax provides an explicit starting version
# This package must use a 2.x version of the Dart SDK starting with 2.14.
sdk: '>=2.14.0 < 3.0.0'
# This package must use a 1.x version of transmogrify starting with 1.4.
To learn more about pub’s version system, see the package versioning page.
A version constraint that uses the traditional syntax can use any of the following values:
|Serves as a explicit declaration of empty version constraint.
|Only the given version
|Limits adoption of your package due the additional limits it places on apps that use your package.
|Given version or later
|Versions later than the given version
|Given version or earlier
|Versions earlier than the given version
|Use this when you know an upper bound version that doesn’t work with your package. This version might be the first to introduce some breaking change.
You can specify any combination of version values as their ranges intersect.
For example, if you set the version value as
this combines the both limitations so the dependency can be any version
Caret syntax expresses the version constraint in a compact way.
^version means the range of all versions guaranteed to be backwards
compatible with the given version.
This range would include all versions up to the next one to introduce a
breaking change. As Dart uses semantic versioning, this would be the next
major version for any package version 1.0 or later
or the next minor version for any package version earlier than 1.0.
|Range covers to
The following example shows caret syntax:
# Covers all versions from 1.3.0 to 1.y.z, not including 2.0.0
# Covers all versions from 1.1.0 to 1.y.z, not including 2.0.0
# Covers all versions from 0.1.2 to 0.1.z, not including 0.2.0
Pub supports two flavors of dependencies: regular dependencies and dev dependencies. Dev dependencies differ from regular dependencies in that dev dependencies of packages you depend on are ignored. Here’s an example:
transmogrify package uses the
test package in its tests and only
in its tests. If someone just wants to use
libraries—it doesn’t actually need
test. In this case, it specifies
test as a dev dependency. Its pubspec will have something like:
test: '>=0.5.0 <0.12.0'
Pub gets every package that your package depends on, and everything those
packages depend on, transitively. It also gets your package’s dev dependencies,
but it ignores the dev dependencies of any dependent packages. Pub only gets
your package’s dev dependencies. So when your package depends on
transmogrify it will get
transmogrify but not
The rule for deciding between a regular or dev dependency is simple: If
the dependency is imported from something in your
it needs to be a regular dependency. If it’s only imported from
example, etc. it can and should be a dev dependency.
Using dev dependencies makes dependency graphs smaller. That makes
faster, and makes it easier to find a set of package versions that satisfies all
You can use
dependency_overrides to temporarily override all references
to a dependency.
For example, perhaps you are updating a local copy of transmogrify, a published package. Transmogrify is used by other packages in your dependency graph, but you don’t want to clone each package locally and change each pubspec to test your local copy of transmogrify.
In this situation, you can override the dependency using
dependency_overrides to specify the directory holding the local
copy of the package.
The pubspec would look something like the following:
You can also use
dependency_overrides to specify a particular
version of a package:
Only the dependency overrides in a package’s own pubspec are considered during package resolution. Dependency overrides inside any depended-on packages are ignored.
As a result, if you publish a package to pub.dev, keep in mind that your package’s dependency overrides are ignored by all users of your package.
It’s important to actively manage your dependencies and ensure that your packages use the freshest versions possible. If any dependency is stale, then you might have not only a stale version of that package, but also stale versions of other packages in your dependency graph that depend on that package. These stale versions can have a negative impact on the stability, performance, and quality of apps.
We recommend the following best practices for package dependencies.
Use caret syntax
Specify dependencies using the caret syntax. This allows the pub tool to select newer versions of the package when they become available. Further, it places an upper bound on the allowed version.
Depend on the latest stable package versions
dart pub upgrade to update to the latest package versions
that your pubspec allows.
To identify dependencies in your app or package that
aren’t on the latest stable versions,
dart pub outdated.
Test whenever you update package dependencies
If you run
dart pub upgrade without updating your pubspec,
the API should stay the same
and your code should run as before—but test to make sure.
If you modify the pubspec and update to a new major version,
then you might encounter breaking changes,
so you need to test even more thoroughly.
Verify the integrity of downloaded packages
When retrieving new dependencies, use the
option to ensure the extracted package content matches
the contents of the original archive.
Without modifying the lockfile,
this flag only resolves new dependencies if:
pubspec.lockis not missing
- The packages’ content hashes match