Start building Flutter web apps on macOS

Verify system requirements

To install and run Flutter, your macOS environment must meet the following hardware and software requirements.

Hardware requirements

Your macOS Flutter development environment must meet the following minimal hardware requirements.

Requirement Minimum Recommended
CPU Cores 4 8
Memory in GB 8 16
Display resolution in pixels WXGA (1366 x 768) FHD (1920 x 1080)
Free disk space in GB 2.5 2.5

Software requirements

To write and compile Flutter code for web, install the following packages.

Operating system

Flutter supports macOS 10.15 (Catalina) or later. This guide presumes your Mac runs the zsh as your default shell.

To verify your shell configuration, expand this section

Like most UNIX-like operating system, macOS can support multiple shells, like bash, zsh, and sh. As of the October 2019 release of macOS Catalina (macOS 10.15), Zsh or zsh is the default shell for macOS.

Check and set zsh as default

  1. To verify zsh was set as the default macOS shell, run the Directory Services command line utility.

     $ dscl . -read ~/ UserShell
    

    should print the following as its response.

     UserShell: /bin/zsh
    

    You can skip the remaining steps.

  2. If you need to install zsh, follow the procedure in this Wiki.

  3. If you need to change your default shell to zsh, run the chsh command.

     $ chsh -s `which zsh`
    

To learn more about macOS and zsh, check out Use zsh as the default shell on your Mac in the macOS documentation.

Some Flutter components require the Rosetta 2 translation process on Macs running Apple silicon. To run all Flutter components on Apple silicon, install Rosetta 2.

$ sudo softwareupdate --install-rosetta --agree-to-license

Development tools

Download and install the following packages.

  • Google Chrome to debug JavaScript code for web apps.* Git 2.27 or later to manage source code. To check if you have git installed, type git version in your Terminal. If you need to install git, type brew install git.

The developers of the preceding software provide support for those products. To troubleshoot installation issues, consult that product’s documentation.

当你在当前版本运行 flutter doctor 的时候,它可能会列出这些软件包的其他不同版本。如果出现这种情况,请安装它推荐的版本。

Text editor or integrated development environment

You can build apps with Flutter using any text editor or integrated development environment (IDE) combined with Flutter’s command-line tools.

Using an IDE with a Flutter extension or plugin provides code completion, syntax highlighting, widget editing assists, debugging, and other features.

Popular options include:

安装 Flutter SDK

你可以使用 VS Code Flutter 扩展或自行下载安装 Flutter 压缩包,来安装 Flutter SDK。

使用 VS Code 安装 Flutter

在使用以下流程来安装 Flutter 前,请确保你已经安装 Visual Studio Code 1.77 或更高版本,以及 Flutter extension for VS Code

根据 VS Code 的提示,安装 Flutter

  1. 启动 VS Code。

  2. 打开 命令面板 (Command Palette),按下快捷键 Command + Shift + P

  3. 命令面板 (Command Palette) 中输入 flutter

  4. 选择 Flutter: New Project

  5. VS Code 会提示你在计算机上找到 Flutter SDK。

    1. 如果你已经安装 Flutter SDK,请单击 Locate SDK

    2. 如果你没有安装 Flutter SDK, 请单击 Download SDK

      如果你没有按照 开发工具的必要条件 安装 Git ,单击该按钮后会导航至 Flutter 安装页面。

  6. 当提示 Which Flutter template? 的时候,请忽略它。可以按下 Esc。你可以在检查完开发配置后创建初始测试项目。

下载 Flutter SDK

  1. 当对话框 Select Folder for Flutter SDK 显示时,选择你想要安装 Flutter 的位置。

    VS Code 会打开你默认的位置。你可以选择其他位置。

    请考虑 ~/development/ 这样的位置

  2. 单击 Clone Flutter

    在下载 Flutter 时,VS Code 会弹出该通知:

    Downloading the Flutter SDK. This may take a few minutes.
    

    下载需要一些时间。如果你怀疑下载被挂起,请单击 Cancel 取消下载后,再重新尝试安装。

  3. Flutter 一旦完成下载, 输出 (Output) 面板就会显示。

    Checking Dart SDK version...
    Downloading Dart SDK from the Flutter engine ...
    Expanding downloaded archive...
    

    成功后,VS Code 会弹出该通知:

    Initializing the Flutter SDK. This may take a few minutes.
    

    初始化时,输出 (Output) 面板会显示下面的内容:

    Building flutter tool...
    Running pub upgrade...
    Resolving dependencies...
    Got dependencies.
    Downloading Material fonts...
    Downloading Gradle Wrapper...
    Downloading package sky_engine...
    Downloading flutter_patched_sdk tools...
    Downloading flutter_patched_sdk_product tools...
    Downloading windows-x64 tools...
    Downloading windows-x64/font-subset tools...
    

    该进程还会运行 flutter doctor -v。此时,请忽略该输出。因为 Flutter Doctor 可能会显示与本次快速启动无关的错误。

    Flutter 安装成功后,VS Code 会弹出该通知:

    Do you want to add the Flutter SDK to PATH so it's accessible
    in external terminals?
    
  4. VS Code 可能会显示一则 Google Analytics 的通知。

    如果你同意,请单击 OK

  5. 在所有 Terminal 窗口中启用 flutter

    1. 关闭,然后重新打开所有 Terminal 窗口。

    2. 重新启动 VS Code。

下载并安装 Flutter

从归档列表中下载 Flutter SDK 压缩包,将压缩包移动到你想要的位置,然后解压 SDK,以此来安装 Flutter。

  1. 下载以下 Flutter SDK 最新 stable 版本的压缩包。

    Intel 处理器   Apple Silicon 处理器
    (获取中…)   (获取中…)

    关于其他发布渠道和旧版本,请查阅 Flutter SDK 归档列表

    Flutter SDK 应该会下载至 macOS 默认下载目录: ~/Downloads/

  2. 创建一个文件夹,用于安装 Flutter。

    可以考虑在 ~/development/ 中创建一个目录。

  3. 将 Flutter SDK 压缩文件 (zip) 解压到你想要存储的目录中。可以使用以下指令进行解压。

    $ unzip ~/development/flutter_sdk_v1.0.0.zip -d ~/development/
    

    完成后,Flutter SDK 应该会位于 ~/development/flutter 目录中。

Add Flutter to your PATH

To run Flutter commands in the Terminal, add Flutter to the PATH environment variable. This guide presumes your Mac runs the latest default shell, zsh. Zsh uses the .zshenv file for environment variables.

  1. Launch your preferred text editor.

  2. If it exists, open the Zsh environmental variable file ~/.zshenv in your text editor. If it doesn’t, create ~/.zshenv.

  3. Copy the following line and paste it at the end of your ~/.zshenv file.

    export PATH=$HOME/development/flutter/bin:$PATH
    
  4. Save your ~/.zshenv file.

  5. To apply this change, restart all open terminal sessions.

If you use another shell, check out this tutorial on setting your PATH.

如果你已经安装了所有必要条件和 Flutter SDK,你应该就可以在 macOS 上为 web 开发 Flutter。

检查你的开发配置

help 帮助

运行 Flutter doctor

flutter doctor 指令将检查 macOS 完整的 Flutter 开发环境的所有组件。

  1. Open 你的 Terminal.

    打开你的 Terminal。

  2. 要检查所有组件的安装情况,请运行以下指令。

    $ flutter doctor
    

由于你选择为 web 进行开发,因此不需要 所有 组件。如果你遵循本指南,指令结果应该类似于:

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.0, on macOS A.B chipset, locale en)
[!] Android toolchain - develop for Android devices
[✓] Chrome - develop for the web
[!] Xcode - develop for iOS and macOS (Xcode not installed)
[!] Android Studio (not installed)
[✓] VS Code (version 1.86)
[✓] Connected device (1 available)
[✓] Network resources

! Doctor found issues in 4 categories.

解决 Flutter doctor 的问题

flutter doctor 指令返回错误时,可能是 Flutter、VS Code、、连接的设备或者网络资源出错。

如果 flutter doctor 指令返回这些组件中的任何一个错误,请使用 verbose 标志再次运行。

$ flutter doctor -v

查看输出结果,了解可能需要安装的其他软件或者需要执行的其他任务。

如果你更改了 Flutter SDK 或其他相关组件的配置,请再次运行 flutter doctor 来检查安装。

管理 Flutter SDK

想要了解管理 Flutter SDK 安装的更多信息,请查阅以下内容。