Mac - Flutter 开发环境搭建

Flutter

Flutter中文官网:https://flutterchina.club
Glutter中文官方安装教程:https://flutterchina.club/get-started/install/

第一步

一. 检查配置

参考:https://flutterchina.club/setup-macos/

要安装并运行Flutter,您的开发环境必须满足以下最低要求:
操作系统: macOS (64-bit) macOS Catalina 10.15.3(及以上)
磁盘空间: 700 MB (不包括Xcode或Android Studio的磁盘空间)

工具: Flutter 依赖下面这些命令行工具.

  1. Xcode 11.3.1(及以上)
  2. bash, mkdir, rm, git, curl, unzip, which

第二步

下载SDK

  1. 使用镜像
    参考:https://flutter.dev/community/china
    执行以下命令将Flutterclone到想要存放Flutter的位置
  2. 使用Git管理FlutterSDK,Flutter To GitHub
$ export PUB_HOSTED_URL=https://pub.flutter-io.cn
$ export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
$ git clone -b dev https://github.com/flutter/flutter.git
$ export PATH="$PWD/flutter/bin:$PATH"
$ cd ./flutter
$ flutter doctor

下载并安装Flutter SDK

Flutter SDK下载
注意,Flutter的渠道版本会不停变动,请以Flutter官网为准。另外,在中国大陆地区,要想正常获取安装包列表或下载安装包,可能需要翻墙,读者也可以去Flutter github项目下去下载安装包,转到下载页。

Mac电脑下载后通常会自动解压为文件夹,
若未自动解压可解压安装包到你想安装的目录,如:

cd ~/development
unzip ~/Downloads/flutter.zip

添加flutter相关工具到path中:

第三步

编辑器

如果有Android Studio 或者 VSCode即可,没有的可以前往下载
Visual Studio Code :https://code.visualstudio.com

配置环境变量

找到用户文件夹,shift + command + . 显示隐藏文件 右键 以文本编辑的形式打开

添加环境变量
参考:官方文档(https://flutterchina.club/setup-macos/) - 更新环境变量
打开终端 输入:open -e .bash_profile

注:若未打开文件或提示文件未找到则,打开文本编辑,新建命名为.bash_profile的文件并保存在用户目录下,并回到终端继续输入 open -e .bash_profile
路径:/Users/***/.bash_profile

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=PATH_TO_FLUTTER_GIT_DIRECTORY/flutter/bin:$PATH

注意:PATH_TO_FLUTTER_GIT_DIRECTORY 为你flutter的路径,比如“~/document/code”
示例:.bash_profile

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

export FLUTTER_HOME=/Users/ming/Documents/flutter
export PATH=$PATH:$FLUTTER_HOME/bin

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

打开终端 运行 source $HOME/.bash_profile 让配置文件立即生效

运行 echo $PATH 检查配置文件,存在刚添加的则配置成功,

终端若使用的是 -zsh,会有一个坑点,运行 echo $PATH 检查配置文件会找不到新加的配置,按照上面步骤打开.zshrc文件 在其中添加:source ~/.bash_profile

一切正常示例:
运行 flutter doctor检查环境,✓ 表示配置成功,✗ 表示未成功

ming@bogon ~ % flutter doctor            
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on macOS 11.3 20E5186d darwin-x64, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device (2 available)

• No issues found!

完成

完成以上操作Flutter 开发环境基本搭建完毕

Flutter更新

要同时更新Flutter SDK和你的依赖包,在你的应用程序根目录(包含pubspec.yaml文件的目录)中运行flutter upgrade 命令:

flutter upgrade
如遇到
在执行 flutter upgrade 时出现 fatal: unable to access 'https://github.com
参照:https://www.jianshu.com/p/72e21df41ac9,进行解决

遇到错误

work@bogon ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.5, on macOS 11.2.3 20D91 darwin-x64, locale
    zh-Hans-CN)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from:
      https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK
      components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup
      for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[!] Xcode - develop for iOS and macOS
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin
        code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see
      https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.
[✗] Chrome - develop for the web (Cannot find Chrome executable at
    /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (not installed)
[✓] VS Code (version 1.55.2)
[!] Connected device
    ! No devices available

解决方案

  1. Pod 没有安装,参照: 进行安装

你可能感兴趣的:(Mac - Flutter 开发环境搭建)