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

系统配置要求

System requirements

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

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

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

    Operating Systems: Windows 7 SP1 or later (64-bit), x86-64 based.

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

    Disk Space: 1.64 GB (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)。

    Extract the zip file and place the contained flutter in the desired installation location for the Flutter SDK (for example, C:\Users\<your-user-name>\Documents).

如果你不想安装指定版本的安装包。可以忽略步骤 1 和 2。从 GitHub 上的 Flutter repo 获取源代码,并根据需要,切换到指定的分支或标签。例如:

If you don’t want to install a fixed version of the installation bundle, you can skip steps 1 and 2. Instead, get the source code from the Flutter repo on GitHub, and change branches or tags as needed. For example:

C:\src>git clone https://github.com/flutter/flutter.git -b stable

现在你可以在控制台当中使用 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 所在的完整路径作为新变量的值。

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

你需要重新打开已经打开的命令行提示符窗口,这样下次启动命令提示符时,才能访问到刚才修改的变量。

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 Command-line Tools, and Android SDK Build-Tools, which are required by Flutter when developing for Android.

  3. 运行 flutter doctor 确保 Flutter 已经定位到了你的 Android Studio 的安装位置。如果 Flutter 并未定位到,运行 flutter config --android-studio-dir <directory> 设置你的 Android Studio 的安装目录。

    Run flutter doctor to confirm that Flutter has located your installation of Android Studio. If Flutter cannot locate it, run flutter config --android-studio-dir <directory> to set the directory that Android Studio is installed to.

配置 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 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_SDK_ROOT 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,点击 AVD Manager 按钮,选择 Create Virtual Device…

    Launch Android Studio, click the AVD Manager icon, and select Create Virtual Device…

    • 在一些旧的 Android Studio 版本里,需要通过 Android Studio > Tools > Android > AVD Manager,然后选择 Create Virtual Device… 选项。(只有在 Android 项目中才会显示 Android 子选项。)

      In older versions of Android Studio, you should instead launch Android Studio > Tools > Android > AVD Manager and select Create Virtual Device…. (The Android submenu is only present when inside an Android project.)

    • 如果你以及还没打开某个项目,你可以选择 Configure > AVD Manager 然后选择 Create Virtual Device 选项

      If you do not have a project open, you can choose Configure > AVD Manager and select Create Virtual Device…

  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.

同意 Android 协议

Agree to Android Licenses

在使用 Flutter 前,你必须同意 Android SDK 平台的协议。你可以在安装完上述工具后执行这一步。

Before you can use Flutter, you must agree to the licenses of the Android SDK platform. This step should be done after you have installed the tools listed above.

  1. 确保你安装了 Java 8,并且正确设置了 JAVA_HOME 环境变量到 JDK 目录。

    Make sure that you have a version of Java 8 installed and that your JAVA_HOME environment variable is set to the JDK’s folder.

    高于 2.2 版本的 Android Studio 自带了 JDK,所以应无需手动操作。

    Android Studio versions 2.2 and higher come with a JDK, so this should already be done.

  2. 打开一个已经提升管理员权限的终端窗口,运行以下命令进行协议的确认。

    Open an elevated console window and run the following command to begin signing licenses.

    $ flutter doctor --android-licenses
    
  3. 仔细阅读每条协议后同意。

    Review the terms of each license carefully before agreeing to them.

  4. 当你同意所有协议后,再次运行 flutter doctor 以确认是否已经可以正常使用 Flutter。

    Once you are done agreeing with licenses, run flutter doctor again to confirm that you are ready to use Flutter.

Windows 设置

Windows setup

其他 Windows 需要的内容

Additional Windows requirements

对于 Windows 桌面开发来说,除了 Flutter SDK 以外你还需要以下内容:

For Windows desktop development, you need the following in addition to the Flutter SDK:

  • Visual Studio 2019(请别把它与 Visual Studio Code 搞混了)做 Win32 开发的话,你需要安装 “Desktop development with C++” 工作负载,包括其所有默认组件。如果是 UWP 开发的话,你需要安装 “Universal Windows Platform development” 工作负载,包括可选的 UWP C++ 工具。

    Visual Studio 2019 (not to be confused with Visual Studio Code). For Win32 you need the “Desktop development with C++” workload installed, including all of its default components. For UWP you need the “Universal Windows Platform development” workload installed, with the optional UWP C++ tools.

启用桌面版支持

Enable desktop support

在命令行中执行以下命令以开启 Win32 桌面端支持:

At the command line, perform the following command to enable Win32 desktop support:

$ flutter config --enable-windows-desktop

Windows UWP 桌面支持需要先升级到 dev 发布渠道,然后启用 UWP。

For Windows UWP desktop support perform the following commands to switch to the dev channel, upgrade Flutter, and enable UWP.

$ flutter channel dev
$ flutter upgrade
$ flutter config --enable-windows-uwp-desktop

更多详情请查看 Flutter 的桌面端支持

For more information, see Desktop support for Flutter

Web 平台设置

Web setup

Flutter 现已在 stable 渠道支持构建 Web 应用。使用 Flutter 2 构建的应用会自动支持 Web 渠道,如果你需要为现有的应用加入 Web 支持,请阅读文档 使用 Flutter 构建 Web 应用

Flutter has support for building web applications in the stable channel. Any app created in Flutter 2 automatically builds for the web. To add web support to an existing app, follow the instructions on Building a web application with Flutter when you’ve completed the setup above.

下一步

Next step

编辑器设置。

Set up your preferred editor.