精彩文章免费看

Mac OS Flutter环境配置详解

系统要求

要安装并运行Flutter,您的开发环境必须满足以下最低要求:
  • 操作系统: macOS (64-bit);
  • 磁盘空间: 700 MB (不包括Xcode或Android Studio的磁盘空间);
  • 工具: Flutter 依赖下面这些命令行工具;
  • bash, mkdir, rm, git, curl, unzip, which

    获取Flutter SDK

    要获得Flutter,请先使用git克隆Flutter,然后将该flutter工具添加到您的用户路径。
    如果这是您第一次在此机器上安装Flutter,请克隆beta分支,然后将该flutter工具添加到您的系统路径中:
    git clone -b beta https://github.com/flutter/flutter.git
    export PUB_HOSTED_URL=https://pub.flutter-io.cn //国内用户需要设置
    export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn //国内用户需要设置
    export PATH=`pwd`/flutter/bin:$PATH
    注意: 以上配置命令为当前终端窗口临时设置PATH变量,你会发现系统重新启动后配置的PATH变量丢失,在终端执行flutter doctor 提示command not found 。所以我们要将Flutter PATH环境变量永久添加到系统路径中,以便可以运行flutter命令在任何终端会话中。
    
    具体步骤如下:

    1.确定您Flutter SDK的目录,您将在步骤3中用到;
    2.在终端打开(或创建) $HOME/.bash_profile. 文件路径和文件名可能在您的机器上不同;
    3.添加以下行并更改[PATH_TO_FLUTTER_GIT_DIRECTORY]为克隆Flutter的git repo的路径;

    下面贴一个笔者本机(mac)的环境变量配置
    //Android sdk 环境变量配置(没配置的需要配置)
    export ANDROID_HOME="/Users/geek/Library/Android/sdk" 
    export PATH=${PATH}:${ANDROID_HOME}/tools
    export PATH=${PATH}:${ANDROID_HOME}/platform-tools
    //Flutter sdk 环境变量配置
    export PUB_HOSTED_URL=https://pub.flutter-io.cn
    export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
    export PATH=/Users/geek/flutter/bin:$PATH
    //注意说明:geek为我本机的用户名,配置时请替换为你自己本机的用户名;注意确认自己Flutter SDK本机路径;
    

    注意: 由于一些flutter命令需要联网获取数据,如果您是在国内访问,由于众所周知的原因,直接访问很可能不会成功。 上面的PUB_HOSTED_URL和FLUTTER_STORAGE_BASE_URL是google为国内开发者搭建的临时镜像。

    以上配置完成后执行以下命令:
    //1.刷新当前终端窗口
    source $HOME/.bash_profile
    //2.查看Flutter配置是否已经在PATH环境变量
    echo $PATH
    
    运行 flutter doctor 显示您可能需要安装的剩余依赖项
    sunnydeMacBook-Pro:cache geek$ flutter doctor
      ╔════════════════════════════════════════════════════════════════════════════╗
      ║ WARNING: your installation of Flutter is 74 days old.                      ║
      ║                                                                            ║
      ║ To update to the latest version, run "flutter upgrade".                    ║
      ╚════════════════════════════════════════════════════════════════════════════╝
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.11.6 15G20015, locale zh-Hans)
    [!] Android toolchain - develop for Android devices (Android SDK 28.0.2)
        ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
    [!] iOS toolchain - develop for iOS devices (Xcode 8.2.1)
        ✗ Flutter requires a minimum Xcode version of 9.0.0.
          Download the latest version or update via the Mac App Store.
        ✗ Brew not installed; use this to install tools for iOS device development.
          Download brew at https://brew.sh/.
    [✓] Android Studio (version 3.1)
    [✓] IntelliJ IDEA Ultimate Edition (version 2018.2)
    [!] Connected devices
    ! Doctor found issues in 3 categories.
    
    一般的错误会是xcode或Android Studio版本太低、或者没有ANDROID_HOME环境变量等,请按照提示解决。本机执行flutter doctor命令后提示老版本,需更新至最新版本;

    更新Flutter SDK 至最新版本

    sunnydeMacBook-Pro:cache geek$ flutter upgrade
    Upgrading Flutter from /Users/geek/flutter...
    From https://github.com/flutter/flutter
       01d6444..ef71d9d  master     -> origin/master
    Already up-to-date.
    Upgrading engine...
    Already up-to-date.
    Flutter 0.5.1 • channel beta • https://github.com/flutter/flutter.git
    Framework • revision c7ea3ca377 (3 months ago) • 2018-05-29 21:07:33 +0200
    Engine • revision 1ed25ca7b7
    Tools • Dart 2.0.0-dev.58.0.flutter-f981f09760
    Running flutter doctor...
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.11.6 15G20015, locale zh-Hans)
    [!] Android toolchain - develop for Android devices (Android SDK 28.0.2)
        ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
    [!] iOS toolchain - develop for iOS devices (Xcode 8.2.1)
        ✗ Flutter requires a minimum Xcode version of 9.0.0.
          Download the latest version or update via the Mac App Store.
        ✗ Brew not installed; use this to install tools for iOS device development.
          Download brew at https://brew.sh/.
    [✓] Android Studio (version 3.1)
    [✓] IntelliJ IDEA Ultimate Edition (version 2018.2)
    [!] Connected devices
    ! Doctor found issues in 3 categories.
    
    遇到打开Android Studio的时候顶部的模拟器一直是loading状态,即使已经打开了模拟器。 运行flutter doctor 提示如下:
    Waiting for another flutter command to release the startup lock
    

    查了一下github的flutter issue 找到了解决方法,如下:
    1、打开flutter的安装目录/bin/cache/ ;
    2、删除lockfile文件(rm -rf lockfle);
    3、重启AndroidStudio;

    Android Studio配置Flutter环境

  • 启动Android Studio.
  • 打开插件首选项 (Preferences>Plugins on macOS,File>Settings>Plugins on Windows & Linux).
  • 选择 Browse repositories…, 选择 Flutter 插件并点击 install.
  • 重启Android Studio后插件生效.
    具体如下图所示:

    WechatIMG84.jpeg
    总结:配置Flutter的过程配置了两次,第一次配置是在当前终端窗口临时设置PATH变量,系统重启后配置丢失了,执行flutter doctor,提示命令无效,打开Android Studio创建Flutter工程一直漫长的等待,就想着是否哪里配置有问题。就又虑一遍配置细节,终于配置完成。以上是所有配置过程,希望能帮到你。