蓝色天空网mbsky.com · 知识 · 实践 · 创造Flutter · 本地阅读预览
教程 / 移动开发 / Flutter

Flutter 安装

核对 Windows、macOS、Linux 三大平台安装 Flutter SDK 的系统要求、Git/VS Code 准备、PATH 环境变量配置、flutter doctor 环境诊断以及常见故障排查思路。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

系统要求与跨平台前置依赖

构建 Flutter 应用前需先在开发机部署 Flutter SDK 及基础工具链。Windows(10 及以上)、macOS(10.14 及以上)与 Linux(Ubuntu 18.04 及以上)均需至少 2.8 GB 可用磁盘空间,并预装 Git(Linux 另需 `curl`、`unzip` 等命令行工具)。本站离线环境校验器实测验证了三平台版本、磁盘下限及缺失工具检出逻辑。

Windows 平台安装与环境变量配置

在 Windows 上推荐按三步准备:第一步,前往 Git for Windows 下载并按默认向导安装 Git(Flutter 依赖 Git 拉取与更新 SDK);第二步,下载并安装推荐编辑器 Visual Studio Code 并启动。

第三步安装 Flutter SDK 提供两条路径:方式一(推荐)是在 VS Code 扩展面板(`Ctrl+Shift+X`)搜索安装 Flutter 扩展,随后按 `Ctrl+Shift+P` 执行 `Flutter: New Project`,选择 `Download SDK` 并指定目标目录即可自动完成配置;方式二是手动从官网下载 `.zip` 压缩包,解压至无空格路径(如 `C:\flutter`),再将 `bin` 目录写入系统 `PATH`。

手动配置 Windows 环境变量时,右键“此电脑”进入“属性”→“高级系统设置”→“环境变量”,在系统变量中编辑 `Path`,新建一条指向 `C:\flutter\bin` 的路径并逐级确定保存,随后重新打开终端使其生效。

macOS 与 Linux 安装流程

在 macOS 上先执行 `git --version` 确认 Git 是否就绪,若未安装则运行 `xcode-select --install` 并在弹窗中确认安装命令行工具;接着安装 VS Code,然后通过 VS Code 引导或手动执行 `git clone https://github.com/flutter/flutter.git -b stable` 拉取稳定分支,并将 `export PATH="$PATH:/Users/<用户名>/flutter/bin"` 追加写入 `~/.zshrc` 后执行 `source ~/.zshrc`。

在 Ubuntu/Debian 等 Linux 发行版上,先运行 `sudo apt-get update` 与 `sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa` 安装基础依赖,再用 `git clone https://github.com/flutter/flutter.git -b stable` 克隆稳定版 SDK,最后将 `$HOME/flutter/bin` 写入 `~/.bashrc` 并执行 `source ~/.bashrc` 刷新环境。

flutter doctor 验证与常见问题排查

完成安装后,在任意新终端运行 `flutter doctor` 检查各目标工具链状态。首次执行时 Flutter 会自动下载匹配的 Dart SDK 并完成初始化,耗时约数分钟;正常情况下输出会以 `[✓]` 标示 Flutter 与已就绪的平台工具链。

排查高频问题可按三类切入:若提示 `flutter` 命令找不到,重点核对 `PATH` 是否指向 `flutter/bin` 以及是否重新打开了终端窗口;若出现 Android Studio 未安装警告但当前仅面向 Web 或桌面端开发,该警告可安全忽略;若确需构建 Android 应用且提示 Android SDK 缺失,则需安装 Android Studio 并通过内置 SDK Manager 补齐对应版本的 Android SDK 与命令行工具。

Flutter 三大操作系统基础系统要求

要求项WindowsmacOSLinux
操作系统Windows 10 或更高版本macOS 10.14 或更高版本Ubuntu 18.04 或更高版本
磁盘空间至少 2.8 GB(不含 IDE/SDK 工具)至少 2.8 GB(不含 Xcode)至少 2.8 GB
必备工具Git for WindowsGit(通过 Xcode 命令行工具)Git、curl、unzip、xz-utils 等

运行 Flutter 系统要求矩阵、跨平台 PATH 解析与 flutter doctor 诊断离线校验

python · 阅读源码
"""Offline Flutter SDK installation, PATH, and doctor verifier for Ch02 (install)."""
import json
import re

VERSION = "CPython 3.12.14 offline Flutter/Dart architecture & runtime verifier (Flutter SDK absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def check_system_requirements(platform_name, os_ver, free_gb, tools):
    min_disk = 2.8
    required_tools = {
        "Windows": {"git"},
        "macOS": {"git"},
        "Linux": {"git", "curl", "unzip"},
    }[platform_name]
    min_os = {"Windows": (10, 0), "macOS": (10, 14), "Linux": (18, 4)}[platform_name]
    missing = sorted(required_tools - set(tools))
    return {
        "os_ok": os_ver >= min_os,
        "disk_ok": free_gb >= min_disk,
        "missing_tools": missing,
        "ready": os_ver >= min_os and free_gb >= min_disk and not missing,
    }


def resolve_path_lookup(path_entries, target_bin):
    for entry in path_entries:
        normalized = entry.replace("\\", "/").rstrip("/")
        if normalized.endswith("/flutter/bin"):
            return f"{normalized}/{target_bin}"
    return None


def parse_doctor_summary(text):
    statuses = {}
    for line in text.splitlines():
        m = re.match(r"^\[([✓!✗])\]\s+(.+)$", line.strip())
        if m:
            mark, label = m.groups()
            statuses[label.split(" (")[0]] = {"✓": "ok", "!": "warning", "✗": "error"}[mark]
    return statuses


def main():
    cases = []

    # Case 1: Cross-platform system requirement validation
    win_ok = check_system_requirements("Windows", (11, 0), 12.5, ["git"])
    mac_ok = check_system_requirements("macOS", (14, 2), 8.0, ["git"])
    linux_missing = check_system_requirements("Linux", (22, 4), 2.5, ["git", "curl"])
    cases.append(
        check(
            "system_requirements_matrix",
            "Verify Windows/macOS/Linux minimum OS version, 2.8 GB disk floor, and prerequisite CLI tools",
            {"win_ready": True, "mac_ready": True, "linux_ready": False, "linux_missing": ["unzip"], "linux_disk_ok": False},
            {
                "win_ready": win_ok["ready"],
                "mac_ready": mac_ok["ready"],
                "linux_ready": linux_missing["ready"],
                "linux_missing": linux_missing["missing_tools"],
                "linux_disk_ok": linux_missing["disk_ok"],
            },
        )
    )

    # Case 2: Windows, macOS (.zshrc), Linux (.bashrc) PATH resolution
    win_path = [r"C:\Windows\System32", r"C:\flutter\bin"]
    mac_path = ["/usr/local/bin", "/Users/dev/flutter/bin"]
    linux_path = ["/usr/bin", "/home/dev/flutter/bin"]
    no_flutter_path = ["/usr/bin", "/bin"]
    cases.append(
        check(
            "path_env_resolution",
            "Verify flutter/bin lookup across Windows, macOS (~/.zshrc), Linux (~/.bashrc), and missing-PATH diagnosis",
            {
                "windows": "C:/flutter/bin/flutter.bat",
                "macos": "/Users/dev/flutter/bin/flutter",
                "linux": "/home/dev/flutter/bin/flutter",
                "missing": None,
            },
            {
                "windows": resolve_path_lookup(win_path, "flutter.bat"),
                "macos": resolve_path_lookup(mac_path, "flutter"),
                "linux": resolve_path_lookup(linux_path, "flutter"),
                "missing": resolve_path_lookup(no_flutter_path, "flutter"),
            },
        )
    )

    # Case 3: Shell command templates (git clone -b stable, apt-get, xcode-select)
    commands = {
        "mac_git_check": "git --version",
        "mac_clt": "xcode-select --install",
        "clone_stable": "git clone https://github.com/flutter/flutter.git -b stable",
        "linux_deps": "sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa",
        "mac_rc": "~/.zshrc",
        "linux_rc": "~/.bashrc",
    }
    cases.append(
        check(
            "install_cli_commands_and_channels",
            "Verify git clone stable channel flag, Linux apt package list, and shell profile targets",
            {
                "branch": "stable",
                "linux_pkg_count": 6,
                "shell_profiles": ["~/.zshrc", "~/.bashrc"],
            },
            {
                "branch": commands["clone_stable"].split("-b ")[1],
                "linux_pkg_count": len(commands["linux_deps"].split("-y ")[1].split()),
                "shell_profiles": [commands["mac_rc"], commands["linux_rc"]],
            },
        )
    )

    # Case 4: flutter doctor output parser and troubleshooting diagnosis
    sample_doctor = """Doctor summary (v3.24.0):
[✓] Flutter (Channel stable, 3.24.0)
[!] Android toolchain (Android Studio not installed)
[✓] Chrome (for web development)
[✓] Windows toolchain
[✓] Web server
"""
    parsed = parse_doctor_summary(sample_doctor)
    can_build_web_without_android = parsed["Flutter"] == "ok" and parsed["Chrome"] == "ok"
    cases.append(
        check(
            "flutter_doctor_diagnosis",
            "Verify flutter doctor status parsing and that Android Studio warning does not block Web/Windows targets",
            {
                "flutter": "ok",
                "android": "warning",
                "chrome": "ok",
                "web_ready_despite_android_warning": True,
            },
            {
                "flutter": parsed["Flutter"],
                "android": parsed["Android toolchain"],
                "chrome": parsed["Chrome"],
                "web_ready_despite_android_warning": can_build_web_without_android,
            },
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证三平台 OS/磁盘/工具准入规则、Windows/macOS/Linux PATH 检索及 flutter doctor 警告分诊逻辑。
运行 Flutter 系统要求矩阵、跨平台 PATH 解析与 flutter doctor 诊断离线校验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:5a2423e22e2cbfa5d445e8169e970bcba0963cce6007a847e5df1b0b59c66433

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Flutter 安装实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:f324b1fcce0793fdb8fe76ef4d7adc80c89dd43c9fa56b45eeaf3ce2a41daee3

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

配置完 Windows 系统环境变量 Path 后,如果在原终端直接输入 `flutter doctor` 仍提示找不到命令,最常见的原因是什么?

展开参考解答

已打开的旧终端进程不会自动重载修改后的系统环境变量,需要关闭并新开终端窗口,同时核对 Path 中填写的是 `flutter\bin` 目录而非 `flutter` 根目录。

规范与官方依据