目录

Contents

dart pub add

Add is one of the commands of the pub tool.

$ dart pub add <package>[:<constraint>] [options]

This command adds the specified package to the pubspec as a dependency, and then gets the dependency. For example, the following command is equivalent to editing pubspec.yaml to add the http package, and then calling dart pub get:

$ dart pub add http

By default, dart pub add uses the latest stable version of the package from the pub.dev site. 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 the pubspec.

To add a dev dependency, use the --dev option:

$ dart pub add --dev test

Options

For options that apply to all pub commands, see Global options.

-d, --dev

Adds the package as a dev dependency, instead of as a regular dependency.

--git-url=<git_repo_url>

Depends on the package in the specified Git repository.

$ dart pub add http --git-url=https://github.com/my/http.git

--git-ref=<branch_or_commit>

With --git-url, depends on the specified branch or commit of a Git repo.

$ dart pub add http --git-url=https://github.com/my/http.git --git-ref=tmpfixes

--git-path=<directory_path>

With --git-url, specifies the location of a package within a Git repo.

--hosted-url=<package_server_url>

Depends on the package server at the specified URL.

--path=<directory_path>

Depends on a locally stored package.

--sdk=<sdk_name>

Depends on a package that’s shipped with the specified SDK (example: --sdk=flutter).

--[no-]offline

默认情况下,pub 会从网络上拉取 package (--no-offline)。若你想使用本地的 package,使用 --offline 参数。想了解更多细节,请查看 离线获取 package.

By default, pub connects to the network to retrieve hosted packages (--no-offline). To use cached packages instead, use --offline. For details, see Getting while offline.

-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.