dart pub add
Use dart pub add to add a dependency.
Add is one of the commands of the pub tool.
$ dart pub add [{dev|override}:]<package>[:<descriptor>] [[{dev|override}:]<package>[:<descriptor>] ...] [options]
This command adds the specified packages to the
pubspec.yaml as dependencies, and then
retrieves the dependencies to resolve
pubspec.yaml.
The following example command is equivalent to editing
pubspec.yaml to add the
http package, and then calling
dart pub get:
dart pub add http
Version constraint
#
By default, dart pub add uses the latest stable
version of the package from the
pub.dev site
that is compatible with your SDK constraints and
dependencies. For example, if 0.13.3 is the
latest stable version of the http package, then
dart pub add http adds
http: ^0.13.3 under
dependencies in your pubspec.yaml.
You can also specify a constraint or constraint range:
dart pub add foo:2.0.0
dart pub add foo:'^2.0.0'
dart pub add foo:'>=2.0.0 <3.0.1'
If the specified package is an existing dependency in your
pubspec.yaml, dart pub add updates
the dependency's constraint to the one specified in the
command.
Dev dependency
#
The dev: prefix adds the package as a
dev dependency, instead of as a regular dependency.
dart pub add dev:foo # adds newest compatible stable version of foo
dart pub add dev:foo:^2.0.0 # adds specified constraint of foo
dart pub add foo dev:bar # adds regular dependency foo and dev dependency bar simultaneously
Previously the -d, --dev option:
dart pub add --dev foo
Dependency override
#
To specify a
dependency override, add the override: prefix and include a
version constraint
or other
package descriptor.
For example: To override all references to
package:foo to use version
1.0.0 of the package, run the following
command:
dart pub add override:foo:1.0.0
This adds the override to your
pubspec.yaml file:
dependency_overrides:
foo: 1.0.0
Package descriptor
#
To specify the version or
dependency source
when adding a dependency, use the
package descriptor
syntax after the package name and a colon. For example, the
following command adds package:foo as a dev
dependency using a git repository as the dependency source:
dart pub add dev:foo:"{git: https://github.com/foo/foo}"
The descriptor syntax allows you to add multiple packages from different sources as dependencies and apply different options and constraints to each.
Options
#For options that apply to all pub commands, see Global options.
--[no-]offline
#
默认情况下,pub 会从网络上拉取 package
(--no-offline)。若你想使用本地的 package,使用
--offline 参数。想了解更多细节,请查看
离线获取 package.
-n, --dry-run
#
Reports which dependencies would change, but doesn't change any.
--[no-]precompile
#
By default, pub precompiles executables in immediate
dependencies (--precompile). To prevent
precompilation, use --no-precompile.
--[no-]example
#
By default, after adding the specified dependencies, pub
also retrieves the dependencies of the
example/ directory
of the package (--example). To skip retrieving
example dependencies, use --no-example.
In a workspace
#
In a Pub workspace
dart pub add will add dependencies only to the
package in the current directory.