搭建Flutter开发环境(Mac OS环境)

本文描述如何在 Mac OS 系统中安装 Flutter 开发环境。

下载安装包

官网下载 Mac OS 版本的安装包。如果访问官网有问题,也可以下载我放在网盘上的包:

链接:https://pan.baidu.com/s/17XysQ8XoY96n03-PQPrQyg
密码:9joe

安装并设置环境

解压下载的安装包。

在本文中,我将其解压在用户目录下的 devel 目录中。

设置系统环境变量: PATH

编辑用户的 .bashrc 文件, 运行:

1
vi .bashrc

在文件末尾加上:

1
export PATH="$PATH:`pwd`/devel/flutter/bin"

注意: 大、小写不能有错。pwd 前后的不是单引号(‘),是`

添加后,保存。再执行

1
source .bashrc

让添加的环境变量立即生效。

验证设置

1
flutter --version

系统提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Flutter 1.9.1+hotfix.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 2d2a1ffec9 (3 weeks ago) • 2019-09-06 18:39:49 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0

╔════════════════════════════════════════════════════════════════════════════╗
║ Welcome to Flutter! - https://flutter.dev ║
║ ║
║ The Flutter tool anonymously reports feature usage statistics and crash ║
║ reports to Google in order to help Google contribute improvements to ║
║ Flutter over time. ║
║ ║
║ Read about data we send with crash reports: ║
║ https://github.com/flutter/flutter/wiki/Flutter-CLI-crash-reporting ║
║ ║
║ See Google's privacy policy: ║
║ https://www.google.com/intl/en/policies/privacy/ ║
║ ║
║ Use "flutter config --no-analytics" to disable analytics and crash ║
║ reporting. ║
╚════════════════════════════════════════════════════════════════════════════╝

使用 Flutter Doctor 检查依赖

使用Flutter doctor 命令检查开发环境所依赖的软件,库是否安装完整。运行

1
flutter doctor

在我的环境中,系统会提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.14.6 18G103, locale
zh-Hans-CN)

[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor
--android-licenses
[!] Xcode - develop for iOS and macOS (Xcode 10.3)
! CocoaPods out of date (1.6.0 is recommended).
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 upgrade:
sudo gem install cocoapods
pod setup
[!] Android Studio (version 3.5)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[!] VS Code (version 1.38.1)
✗ Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[!] Connected device
! No devices available

可以看到,flutter doctor 分别检测出了:1. Android SDK 的问题;2. iOS 开发中 Cocoapods 的问题;3. Android Studio 没有安装插件的问题;4. VS Code 没有安装插件的问题。 并都提出了解决方法。接下来就是安装解决方法去解决。

  1. 运行 “flutter doctor –android-licenses”, 按照系统提示同意所有的协议。

  2. 运行 “sudo gem install cocoapods”, 安装 cocopods。

  3. 在 Android Studio 中安装 Dart 插件

  4. 在 VS Code 中安装 Flutter 插件

安装完以后再次运行 “flutter doctor” 就没有问题了

设置依赖包仓库的国内镜像

默认的依赖包仓库在国外,从国内访问速度很慢,有时甚至不能访问。因此建议国内的开发人员把包仓库地址设置为国内的镜像地址。在 .bashrc 文件中加入一下内容:

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

或是:

1
2
export FLUTTER_STORAGE_BASE_URL: https://mirrors.sjtug.sjtu.edu.cn/
export PUB_HOSTED_URL: https://dart-pub.mirrors.sjtug.sjtu.edu.cn/

在执行:

1
source ~/.bashrc

然更改立即生效。

本文标题:搭建Flutter开发环境(Mac OS环境)

文章作者:Morning Star

发布时间:2019年09月28日 - 07:09

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/app/flutter/flutter-development-environment-mac/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。