跳转至正文

将 Flutter 添加到 PATH

了解在下载 Flutter SDK 后如何将 Flutter 添加到你的 PATH。

了解如何在下载 SDK 后将 Flutter 添加到你的 PATH 环境变量。将 Flutter 添加到你的 PATH 后,你就可以在终端和 IDE 中使用 flutterdart 命令行工具。

Windows

#

Windows

#

要在 Windows 终端中运行 flutterdart 命令,请将 Flutter SDK 的 bin 目录添加到 Path 环境变量。

  1. Determine your Flutter SDK installation location

    Copy the absolute path to the directory that you downloaded and extracted the Flutter SDK into.

    1. 按下快捷键 Windows + Pause

      如果你的键盘上没有 Pause 键,请尝试 Windows + Fn + B

      这将会显示 系统 > 系统信息 的窗口。

    2. 单击 高级系统设置 > 高级 > 环境变量…

      这将会显示 环境变量 的窗口。

  2. Add the Flutter SDK bin to your path

    1. In the User variables for (username) section of the Environment Variables dialog, look for the Path entry.

    2. If the Path entry exists, double-click it.

      The Edit Environment Variable dialog should open.

      1. Double-click inside an empty row.

      2. Type the path to the bin directory of your Flutter installation.

        For example, if you downloaded Flutter into a develop\flutter folder inside your user directory, you'd type the following:

        %USERPROFILE%\develop\flutter\bin
        
      3. Click the Flutter entry you added to select it.

      4. Click Move Up until the Flutter entry sits at the top of the list.

      5. To confirm your changes, click OK three times.

    3. If the entry doesn't exist, click New....

      The Edit Environment Variable dialog should open.

      1. In the Variable Name box, type Path.

      2. In the Variable Value box, type the path to the bin directory of your Flutter installation.

        For example, if you downloaded Flutter into a develop\flutter folder inside your user directory, you'd type the following:

        %USERPROFILE%\develop\flutter\bin
        
      3. To confirm your changes, click OK three times.

  3. Apply your changes

    To apply this change and get access to the flutter tool, close and reopen all open command prompts, sessions in your terminal apps, and IDEs.

  4. Validate your setup

    To ensure you successfully added the SDK to your PATH, open command prompt or your preferred terminal app, then try running the flutter and dart tools.

    flutter --version
    dart --version
    

    If either command isn't found, check out Flutter installation troubleshooting.

macOS

#

macOS

#

要在 macOS 终端中运行 flutterdart 命令,请将 Flutter SDK 的 bin 目录添加到 PATH 环境变量。

  1. 确定 Flutter SDK 安装位置

    复制你下载并解压 Flutter SDK 所在目录的绝对路径。

  2. 打开或创建 Zsh 环境变量文件

    若存在,请在你偏好的文本编辑器中打开 Zsh 环境变量文件 ~/.zprofile。若不存在,请创建 ~/.zprofile 文件。

  3. 将 Flutter SDK 的 bin 添加到你的 PATH

    ~/.zprofile 文件末尾,使用内置 export 命令更新 PATH 变量,使其包含 Flutter 安装目录下的 bin

    <path-to-sdk> 替换为你的 Flutter SDK 安装路径。

    bash
    export PATH="<path-to-sdk>/bin:$PATH"
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可向文件添加:

    bash
    export PATH="$HOME/develop/flutter/bin:$PATH"
    
  4. 保存你的更改

    保存并关闭你编辑的 ~/.zprofile 文件。

  5. 应用你的更改

    要应用此更改并访问 flutter 工具,请在终端应用和 IDE 中关闭并重新打开所有已打开的 Zsh 会话。

  6. 验证你的配置

    为确保你已成功将 SDK 添加到 PATH,请在你偏好的终端中打开 Zsh 会话,然后尝试运行 flutterdart 工具。

    flutter --version
    dart --version
    

    若任一命令未找到,请参阅 Flutter 安装问题排查

Linux

#

Linux

#

要在 Linux 终端中运行 flutterdart 命令,请将 Flutter SDK 的 bin 目录添加到 PATH 环境变量。

  1. 确定 Flutter SDK 安装位置

    复制你下载并解压 Flutter SDK 所在目录的绝对路径。

  2. 确定你的默认 shell

    若你不确定自己使用哪种 shell,可查看打开新控制台窗口时启动的是哪一种。

    echo $SHELL
    
  3. 将 Flutter SDK 的 bin 添加到你的 PATH

    要将 Flutter 安装目录下的 bin 添加到 PATH

    1. Expand the instructions for your default shell.
    2. 展开与你默认 shell 对应的说明。
    3. Copy the provided command.
    4. 复制提供的命令。
    5. Replace <path-to-sdk> with the path to your Flutter SDK install.
    6. <path-to-sdk> 替换为你的 Flutter SDK 安装路径。
    7. Run the edited command in your preferred terminal with that shell.
    8. 在使用该 shell 的你偏好的终端中运行编辑后的命令。

    Expand for bash instructions
    展开 bash 相关说明
    echo 'export PATH="<path-to-sdk>/bin:$PATH"' >> ~/.bashrc
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'export PATH="$HOME/develop/flutter/bin:$PATH"' >> ~/.bashrc
    
    Expand for zsh instructions
    展开 zsh 相关说明
    echo 'export PATH="<path-to-sdk>/bin:$PATH"' >> ~/.zshenv
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'export PATH="$HOME/develop/flutter/bin:$PATH"' >> ~/.zshenv
    
    Expand for fish instructions
    展开 fish 相关说明
    fish_add_path -g -p <path-to-sdk>/bin
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    fish_add_path -g -p ~/develop/flutter/bin
    
    Expand for csh instructions
    展开 csh 相关说明
    echo 'setenv PATH "<path-to-sdk>/bin:$PATH"' >> ~/.cshrc
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'setenv PATH "$HOME/develop/flutter/bin:$PATH"' >> ~/.cshrc
    
    Expand for tcsh instructions
    展开 tcsh 相关说明
    echo 'setenv PATH "<path-to-sdk>/bin:$PATH"' >> ~/.tcshrc
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'setenv PATH "$HOME/develop/flutter/bin:$PATH"' >> ~/.tcshrc
    
    Expand for ksh instructions
    展开 ksh 相关说明
    echo 'export PATH="<path-to-sdk>/bin:$PATH"' >> ~/.profile
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'export PATH="$HOME/develop/flutter/bin:$PATH"' >> ~/.profile
    
    Expand for sh instructions
    展开 sh 相关说明
    echo 'export PATH="<path-to-sdk>/bin:$PATH"' >> ~/.profile
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'export PATH="$HOME/develop/flutter/bin:$PATH"' >> ~/.profile
    
  4. 应用你的更改

    要应用此更改并访问 flutter 工具,请在终端应用和 IDE 中关闭并重新打开所有已打开的 shell 会话。

  5. 验证你的配置

    为确保你已成功将 SDK 添加到 PATH,请使用默认 shell 打开你偏好的终端,然后尝试运行 flutterdart 工具。

    flutter --version
    dart --version
    

    若任一命令未找到,请参阅 Flutter 安装问题排查

ChromeOS

#

ChromeOS

#

要在 ChromeOS 终端中运行 flutterdart 命令,请将 Flutter SDK 的 bin 目录添加到 PATH 环境变量。

  1. 确定 Flutter SDK 安装位置

    复制你下载并解压 Flutter SDK 所在目录的绝对路径。

  2. 将 Flutter SDK 的 bin 添加到你的 PATH

    要将 Flutter 安装目录下的 bin 添加到 PATH

    1. Copy the following command.
    2. 复制以下命令。
    3. Replace <path-to-sdk> with the path to your Flutter SDK install.
    4. <path-to-sdk> 替换为你的 Flutter SDK 安装路径。
    5. Run the edited command in your preferred terminal.
    6. 在你偏好的终端中运行编辑后的命令。
    echo 'export PATH="<path-to-sdk>:$PATH"' >> ~/.bash_profile
    

    例如,若你将 Flutter 下载到用户目录下的 develop/flutter 文件夹,可运行:

    echo 'export PATH="$HOME/develop/flutter/bin:$PATH"' >> ~/.bash_profile
    
  3. 应用你的更改

    要应用此更改并访问 flutter 工具,请在终端应用和 IDE 中关闭并重新打开所有已打开的 Zsh 会话。

  4. 验证你的配置

    为确保你已成功将 SDK 添加到 PATH,请在你偏好的终端中打开 Zsh 会话,然后尝试运行 flutterdart 工具。

    flutter --version
    dart --version
    

    若任一命令未找到,请参阅 Flutter 安装问题排查