目录

Contents

dart pub 工具

The pub package manager has a command-line interface that works with either the flutter tool or the dart tool. With either tool, add the pub command followed by a subcommand such as get:

$ dart pub get    # Gets dependencies for a non-Flutter package
$ flutter pub get # Gets dependencies for a Flutter package

This site uses dart pub <subcommand> for its examples, but if your current directory holds a Flutter app or other Flutter-specific code, use flutter pub <subcommand> instead. For more information, see Using packages on the Flutter website.

If you encounter problems using the pub tool, see Troubleshooting Pub.

List of subcommands

Detailed documentation exists for each of the following pub subcommands:

Overview of subcommands

Pub 的命令可以分为以下几类:

Pub’s subcommands fall into the following categories:

管理 Package 的依赖关系

Managing package dependencies

Pub 提供了许多命令,这些命令用于管理那些 你代码所依赖 Package

Pub provides a number of subcommands for managing the packages your code depends on.

在这些命令中,最常用的是 get 命令和 upgrade 命令,前者用于检索 Package 的依赖项,后者用于更新 Package 的依赖项。每一次你修改了 pubspec 文件后都需要执行 dart pub getflutter pub get 命令来确保你所用的依赖项是最新的。一些 IDE 会在创建项目或修改了 pubspec 文件后自动执行此操作。

In this group, the most commonly used subcommands are get and upgrade, which retrieve or upgrade dependencies used by a package. Every time you modify a pubspec file, run dart pub get or flutter pub get to make sure the dependencies are up to date. Some IDEs perform this step automatically on the creation of a project, or any modification of the pubspec.

cache
Manages pub’s local package cache. Use this subcommand to add packages to your cache, or to perform a clean reinstall of all packages in your cache.

cache 命令
用于管理 Pub 的本地 Package 缓存。使用该命令你可以将一个 Package 添加至缓存,或者清除所有缓存的 Package 并重新安装。

deps
Lists all dependencies used by the current package.

deps 命令
用于显示当前 Package 使用的所有依赖项。

downgrade
Retrieves the lowest versions of all the packages that are listed as dependencies used by the current package. Used for testing the lower range of your package’s dependencies.

downgrade 命令
用于检索当前 Package 所依赖的其它 Package 的最低版本。用于测试这些较低版本依赖项的 Package 在当前 Package 上的兼容性。

get
Retrieves the packages that are listed as the dependencies for the current package. If a pubspec.lock file already exists, fetches the version of each dependency (if possible) as listed in the lock file. Creates or updates the lock file, as needed.

get 命令
用于检索当前 Package 所依赖的其它 Package。如果 pubspec.lock 文件已经存在,则根据该文件中保存的依赖项版本获取对应的依赖项。如有必要,将会创建或更新该文件。

outdated
Looks at every package that the current package depends on, determines which package dependencies are out of date, and gives you advice on how to update them. Use this subcommand when you want to update package dependencies.

outdated 命令
查看当前软件包所依赖的每个 package,确定哪些 package 的依赖项已过时,并为您提供有关如何更新它们的建议。当您要更新 package 的依赖性时,请使用此命令。

upgrade
Retrieves the latest version of each package listed as dependencies used by the current package. If a pubspec.lock file exists, ignores the versions listed in the lock file and fetches the newest versions that honor the constraints in the pubspec. Creates or updates the lock file, as needed.

upgrade 命令
用于检索当前 Package 所依赖的其它 Package 的最新版本。如果 pubspec.lock 文件已经存在,则忽略其保存的版本并以 pubspec 文件中指定的最新版本为主。如有必要,将会创建或更新该文件。

运行命令行应用

Running command-line apps

The global subcommand lets you make a package globally available, so you can run scripts from that package’s bin directory. To run globally available scripts, you must add the system cache bin directory to your path.

Deploying packages and apps

部署 Package 和应用

使用 pub 命令你还可以发布 Package 和命令行应用。

With pub you can publish packages and command-line apps.

Packages

Package

你可以使用 publish 命令将 Package 上传至 Pub 网站以分享给全世界的开发者使用。 uploader 命令则可以允许指定用户修改 Package 和上传新版本的 Package。

To share your Dart packages with the world, you can use the publish subcommand to upload the package to the pub.dev site. The uploader subcommand enables specific users to modify and upload new versions of your package.

命令行应用

Command-line apps

任何包含脚本(即在 bin/ 目录下有任意文件)的 Package,可以在 pubspec 文件中添加上 executables 标签。当一个脚本标识为 executables 时,用户可以直接从命令行使用 pub global activate 命令运行它。

For any package that contains scripts (anything under the bin/ directory), consider adding the executables tag to the pubspec file. When a script is listed under executables, users can run dart pub global activate to make it directly available from the command line.

全局选项

Global options

有几个命令行选线可以用于所有 pub 命令。它们包括:

Several command-line options work with all of the pub subcommands. These include:

--help-h
显示使用说明。

--help or -h
Print usage information.

--version
显示当前 pub 命令的版本。

--version
Print version of pub.

--trace
当出现错误时输出调试信息。

--trace
Print debugging information when an error occurs.

--verbosity=<level>
指定输出信息的级别:

--verbosity=<level>
The specified level determines the amount of information that is displayed:

  • all:显示所有出输出,包括内部追踪信息。

    all
    Show all output, including internal tracing messages.

  • io:显示 I/O 操作。

    io
    Show I/O operations.

  • normal:显示错误、警告以及用户信息。

    normal
    Show errors, warnings, and user messages.

  • solver:显示版本解析的步骤。

    solver
    Show steps during version resolution.

-verbose-v
等同于 --verbosity=all

-verbose or -v
Equivalent to --verbosity=all.