Add macOS as a target platform for Flutter from web start

To add macOS as a Flutter app target for macOS, follow this procedure.

Install Xcode

#
  1. Allocate a minimum of 26 GB of storage for Xcode. Consider allocating 42 GB of storage for an optimal configuration.
  2. Install Xcode 15 to debug and compile native Swift or ObjectiveC code.

配置 iOS 开发

#

安装并配置 Xcode

#

要为 macOS 开发 Flutter 应用,请安装 Xcode,以便编译为原生字节码。

  1. 打开 App Store 并登录。

  2. 搜索 Xcode

  3. 点击 获取 (Install)

    Xcode 安装程序占用 6 GB 以上的存储空间。下载会需要一些时间。

  4. 请运行以下命令,来配置命令行工具使用已安装的 Xcode 版本。

    sudo sh -c 'xcode-select -s /Applications/Xcode.app/Contents/Developer && xcodebuild -runFirstLaunch'

    使用以上路径可以获取最新版本的 Xcode。如果你需要使用其他版本,请自行指定该路径。

  5. 签署 Xcode 许可证协议。

    sudo xcodebuild -license

请尽量使用最新版本的 Xcode。

安装 CocoaPods

#

如果你的应用程序依赖于带有原生 macOS 代码的 Flutter 插件,请安装 CocoaPods。该程序会捆绑 Flutter 和 macOS 代码之间的各种依赖关系。

请运行以下步骤,安装并设置 CocoaPods:

  1. 按照 CocoaPods 安装指南 安装 cocoapods

    sudo gem install cocoapods
  2. 启动你喜欢的文本编辑器。

  3. 在文本编辑器中打开 Zsh 环境变量文件 ~/.zshenv

  4. 复制以下内容并粘贴到 ~/.zshenv 文件内的末尾。

    bash
    export PATH=$HOME/.gem/bin:$PATH
  5. 保存 ~/.zshenv 文件。

  6. 请重新启动所有打开的终端会话窗口,来应用此更改。

检查你的开发配置

#

help 帮助

运行 Flutter doctor

#

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

  1. Open 你的 Terminal.

    打开你的 Terminal。

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

    flutter doctor

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

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.4.0 23E214 darwin-arm64, locale en)
[!] Android toolchain - develop for Android devices
[✓] Chrome - develop for the web
[✓] Xcode - develop for iOS and macOS (Xcode 15)
[!] Android Studio (not installed)
[✓] VS Code (version 1.89)
[✓] Connected device (1 available)
[✓] Network resources


! Doctor found issues in 2 categories.

解决 Flutter doctor 的问题

#

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

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

flutter doctor -v

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

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

开始使用 Flutter 开发 macOS desktop 应用

#

恭喜你!现在,你已经安装了所有必要条件和 Flutter SDK,你可以开始为 macOS desktop 开发 Flutter 应用了。