目錄

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 的命令可以分為以下幾類:

管理 Package 的依賴關係

Pub 提供了許多命令,這些命令用於管理那些 你程式碼所依賴 Package

在這些命令中,最常用的是 get 命令和 upgrade 命令,前者用於檢索 Package 的依賴項,後者用於更新 Package 的依賴項。每一次你修改了 pubspec 檔案後都需要執行 dart pub getflutter pub get 命令來確保你所用的依賴項是最新的。一些 IDE 會在建立專案或修改了 pubspec 檔案後自動執行此操作。

cache 命令
用於管理 Pub 的本地 Package 快取。使用該命令你可以將一個 Package 新增至快取,或者清除所有快取的 Package 並重新安裝。

deps 命令
用於顯示當前 Package 使用的所有依賴項。

downgrade 命令
用於檢索當前 Package 所依賴的其它 Package 的最低版本。用於測試這些較低版本依賴項的 Package 在當前 Package 上的相容性。

get 命令
用於檢索當前 Package 所依賴的其它 Package。如果 pubspec.lock 檔案已經存在,則根據該檔案中儲存的依賴項版本獲取對應的依賴項。如有必要,將會建立或更新該檔案。

outdated 命令
檢視當前軟體包所依賴的每個 package,確定哪些 package 的依賴項已過時,併為您提供有關如何更新它們的建議。當您要更新 package 的依賴性時,請使用此命令。

upgrade 命令
用於檢索當前 Package 所依賴的其它 Package 的最新版本。如果 pubspec.lock 檔案已經存在,則忽略其儲存的版本並以 pubspec 檔案中指定的最新版本為主。如有必要,將會建立或更新該檔案。

執行命令列應用

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 和命令列應用。

Packages

Package

你可以使用 publish 命令將 Package 上傳至 Pub 網站以分享給全世界的開發者使用。 uploader 命令則可以允許指定使用者修改 Package 和上傳新版本的 Package。

命令列應用

任何包含指令碼(即在 bin/ 目錄下有任意檔案)的 Package,可以在 pubspec 檔案中新增上 executables 標籤。當一個指令碼標識為 executables 時,使用者可以直接從命令列使用 pub global activate 命令執行它。

全域選項

有幾個命令列選線可以用於所有 pub 命令。它們包括:

--help-h

顯示使用說明。

--trace

當出現錯誤時輸出除錯資訊。

-verbose-v

等同於 --verbosity=all

--directory=<dir>-C <dir>

在指定的目錄下執行命令。

--[no-]color

在輸出內容中加入顏色來增強顯示效果 (使用 --color)。預設值在不同程式設計環境中是不同的,取決於是否在終端中使用,如果在終端中使用 --color 是預設的,其他環境預設是 --no-color。使用 --no-color 可以在所有環境中禁用色彩顯示。