在 Windows 操作系统上安装和配置 Flutter 开发环境

系统配置要求

System requirements

要想安装和运行 Flutter,你的开发环境至少应该满足如下的需求:

To install and run Flutter, your development environment must meet these minimum requirements:

  • 操作系统:Windows 7 SP1 或更高的版本(64 位操作系统)。

    Operating Systems: Windows 7 SP1 or later (64-bit)

  • 磁盘空间:除安装 IDE 和一些工具之外还应有至少 400 MB 的空间。

    Disk Space: 400 MB (does not include disk space for IDE/tools).

  • 工具:要让 Flutter 在你的开发环境中正常使用,依赖于以下的工具:

    Tools: Flutter depends on these tools being available in your environment.

    • Windows PowerShell 5.0 或者更高的版本(Windows 10 中已经预装了)

      Windows PowerShell 5.0 or newer (this is pre-installed with Windows 10)

    • Git for Windows 2.x,并且勾选从 Windows 命令提示符使用 Git 选项。

      Git for Windows 2.x, with the Use Git from the Windows Command Prompt option.

      如果 Windows 版的 Git 已经安装过了,那么请确保能从命令提示符或者 PowerShell 中直接执行 git 命令。

      If Git for Windows is already installed, make sure you can run git commands from the command prompt or PowerShell.

获取 Flutter SDK

Get the Flutter SDK

下载安装包

  1. 下载下面的安装包以获取最新版本 stable 的 Flutter SDK

    Download the following installation bundle to get the latest stable release of the Flutter SDK:

    (loading…)

    要查看其他发行通道和以往的版本,请参阅 SDK 版本列表 页面。

    For other release channels, and older builds, see the SDK releases page.

  2. 将压缩包解压,然后把其中的 flutter 目录整个放在你预想的 Flutter SDK 安装目录中(比如 C:\src\flutter;请勿将该目录放在一些需要额外操作权限的目录,比如 C:\Program Files\)。

    Extract the zip file and place the contained flutter in the desired installation location for the Flutter SDK (for example, C:\src\flutter; do not install Flutter in a directory like C:\Program Files\ that requires elevated privileges).

  3. 找到 flutter 目录中的 flutter_console.bat 文件,双击执行该批处理脚本。

    Locate the file flutter_console.bat inside the flutter directory. Start it by double-clicking.

命令行方式安装

  • 打开命令行窗口,cd 到你想安装 Flutter SDK 的目录,(比如 C:\src\flutter,请勿将该目录放在一些需要额外操作权限的目录,比如 C:\Program Files\)。

    At the command line, go to the desired installation location for the Flutter SDK (for example, C:\src\flutter; do not install Flutter in a directory like C:\Program Files\ that requires elevated privileges).

  • 运行 git clone 命令,下面的命令是用来安装稳定构建渠道的 Flutter SDK,要升级查看其他构建渠道的 Flutter 版本,请参阅 升级 Flutter

    Clone the repo using git clone. The following example clones the stable channel. (To see Flutter’s other release channels, see upgrading Flutter.)

git clone -b stable https://github.com/flutter/flutter.git
  • 定位并打开 flutter 的文件夹,双击运行 flutter_console.bat 开始安装。

    In Windows Explorer, navigate to the flutter directory and locate the flutter_console.bat file. Double-click the file to start it.

现在你可以在控制台当中使用 Flutter 的命令了。

You are now ready to run Flutter commands in the Flutter Console!

更新 path 环境变量

Update your path

如果你想要在普通的 Windows 控制台中使用 Flutter 命令,那就需要按照下面的步骤来将 Flutter 的二进制文件路径加入到 PATH 环境变量。

If you wish to run Flutter commands in the regular Windows console, take these steps to add Flutter to the PATH environment variable:

  • 在开始菜单的搜索功能键入“env”,然后选择 编辑当前用户的环境变量

    From the Start search bar, enter ‘env’ and select Edit environment variables for your account.

  • 用户变量 一栏中,检查是否有 Path 这个条目:

    Under User variables check if there is an entry called Path:

    • 如果存在,直接把 flutter\bin 目录的完整路径以 ; 作为分隔加到已有的值后面。

      If the entry exists, append the full path to flutter\bin using ; as a separator from existing values.

    • 如果不存在的话,在用户环境变量中创建一个新的 Path 变量,然后将 flutter\bin 所在的完整路径作为新变量的值。

      IIf the entry doesn’t exist, create a new user variable named Path with the full path to flutter\bin as its value.

注意,你需要关闭和重新启动已经打开的各个控制台窗口,这样下次启动控制台时才能访问到刚才修改的变量。

Note that you have to close and reopen any existing console windows for these changes to take effect.

运行 flutter doctor

Run flutter doctor

在将 Path 变量更新后,打开一个新的控制台窗口,然后将下面的命令输入进去执行。如果它提示有任何的平台相关依赖,那么你就需要按照指示完成这些配置:

From a console window that has the Flutter directory in the path (see above), run the following command to see if there are any platform dependencies you need to complete the setup:

C:\src\flutter>flutter doctor

上述命令会检查你的现有环境,然后把检测结果以报告形式呈现出来。仔细阅读它显示的内容,检查是否有尚未安装的软件或是有其他的步骤需要完成(通常会以粗体呈现)。

This command checks your environment and displays a report of the status of your Flutter installation. Check the output carefully for other software you might need to install or further tasks to perform (shown in bold text).

举个例子:

For example:

[-] Android toolchain - develop for Android devices
    • Android SDK at D:\Android\sdk
    ✗ Android SDK is missing command line tools; download from https://goo.gl/XxQghQ
    • Try re-installing or updating your Android SDK,
      visit https://flutter.cn/setup/#android-setup for detailed instructions.

下面的章节描述了如何执行报告中提到的任务来完成整体配置。每当按照报告安装了任何的依赖项,就可以执行 flutter doctor 来检查还有什么没有完成的。

The following sections describe how to perform these tasks and finish the setup process. Once you have installed any missing dependencies, you can run the flutter doctor command again to verify that you’ve set everything up correctly.

设置 Android 开发环境

Android setup

安装 Android Studio

Install Android Studio

  1. 下载并安装 Android Studio

    Download and install Android Studio.

  2. 运行 Android Studio,并进入 ‘Android Studio Setup Wizard’,这会安装最新的 Android SDK, Android SDK Platform-Tools 以及 Android SDK Build-Tools,这些都是在开发 Android Flutter 应用时所需要的。

    Start Android Studio, and go through the ‘Android Studio Setup Wizard’. This installs the latest Android SDK, Android SDK Platform-Tools, and Android SDK Build-Tools, which are required by Flutter when developing for Android.

配置 Android 设备

Set up your Android device

在 Android 设备上运行或测试 Flutter 应用之前,你需要一个运行 Android 4.1(API 版本 16)或者更高的设备。

To prepare to run and test your Flutter app on an Android device, you’ll need an Android device running Android 4.1 (API level 16) or higher.

  1. 在设备上打开 Developer optionsUSB debugging 选项,你可以在 Android documentation 上查看更详细的方法介绍。

    Enable Developer options and USB debugging on your device. Detailed instructions are available in the Android documentation.

  2. 如果是在 Windows 平台上使用,需要安装 Google USB Driver

    Windows-only: Install the Google USB Driver.

  3. 通过 USB 接口连接手机和电脑,如果在设备上弹出需要授权弹窗,允许授权以便让电脑能够访问你的开发设备。

    Using a USB cable, plug your phone into your computer. If prompted on your device, authorize your computer to access your device.

  4. 在命令行中,使用 flutter devices 命令来确保 Flutter 能够识别出你所连接的 Android 设备。

    In the terminal, run the flutter devices command to verify that Flutter recognizes your connected Android device. By default, Flutter uses the version of the Android SDK where your adb tool is based. If you want Flutter to use a different installation of the Android SDK, you must set the ANDROID_HOME environment variable to that installation directory.

默认情况下,Flutter 会使用当前版本 adb 工具所依赖的 Android SDK 版本,如果你想让 Flutter 使用别的 Android SDK,你可以通过设置 ANDROID_HOME 环境变量来达到目的。

By default, Flutter uses the version of the Android SDK where your adb tool is based. If you want Flutter to use a different installation of the Android SDK, you must set the ANDROID_HOME environment variable to that installation directory.

配置 Android 模拟器

Set up the Android emulator

根据以下步骤来将 Flutter 应用运行或测试于你的 Android 模拟器上:

To prepare to run and test your Flutter app on the Android emulator, follow these steps:

  1. 激活机器上的 VM acceleration 选项。

    Enable VM acceleration on your machine.

  2. 启动 Android Studio > Tools > Android > AVD Manager,然后选择 Create Virtual Device 选项。(只有在 Android 项目中才会显示 Android 子选项。)

    Launch Android Studio > Tools > Android > AVD Manager and select Create Virtual Device. (The Android submenu is only present when inside an Android project.)

  3. 选择相应的设备并选择 Next 选项。

    Choose a device definition and select Next.

  4. 选择一个或多个你想要模拟的 Android 版本的系统镜像,然后选择 Next 选项。推荐选择 x86 或者 x86_64 镜像。

    Select one or more system images for the Android versions you want to emulate, and select Next. An x86 or x86_64 image is recommended.

  5. 在 Emulated Performance 下选择 Hardware - GLES 2.0 选项来开启 硬件加速

    Under Emulated Performance, select Hardware - GLES 2.0 to enable hardware acceleration.

  6. 确保 AVD 选项配置正确,并选择 Finish 选项。

    Verify the AVD configuration is correct, and select Finish.

    想要查看上述步骤的更多详细信息,请查看 Managing AVDs 页面。

    For details on the above steps, see Managing AVDs.

  7. 在 Android Virtual Device Manager 中,点击工具栏中的 Run 选项,模拟器会启动并为你所选择的系统版本和设备显示出相应的界面。

    In Android Virtual Device Manager, click Run in the toolbar. The emulator starts up and displays the default canvas for your selected OS version and device.

Web 平台设置

Web setup

如果要在 Web 平台运行 Flutter 应用,你必须 安装 Chrome 浏览器

To prepare to run and test your Flutter app on the web, you must install Chrome, if you haven’t already.

下一步

Next step

编辑器设置。

Set up your preferred editor.