MacOS安装flutter

Flutter在2018年横空出世,吸引了很多前端人员的目光,是继RN之后又一个APP开发语言,且在谷歌的巨大光环下孕育,自然倍受关注。作为一枚前端开发人员,也想捣腾下Flutter。

下面是我在MAC上安装Flutter的一些经历。

获取Flutter SDK

克隆flutter分支

如果这是您第一次在此机器上安装Flutter,请克隆beta分支,然后将该flutter工具添加到您的系统路径中:

1
2
3
4
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

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

运行 flutter doctor

运行以下命令查看是否需要安装其它依赖项来完成安装:

1
flutter doctor

该命令检查您的环境并在终端窗口中显示报告。Dart SDK已经在捆绑在Flutter里了,没有必要单独安装Dart。

执行上述命令大概率情况下是会报错的,如果没有报错恭喜你太顺了。我这里就报错了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[!] Android toolchain - develop for Android devices (Android SDK 25.0.2)
✗ Android license status unknown.
[!] iOS toolchain - develop for iOS devices (Xcode 9.2)
✗ Xcode requires additional components to be installed in order to run.
Launch Xcode and install additional required components when prompted.
✗ libimobiledevice and ideviceinstaller are not installed. To install, run:
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 2.3)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[!] Connected devices
! No devices available

! Doctor found issues in 3 categories.

错误分三类:Android toolchain,iOS toolchain,Android Studio。关于ios的错误,我们按照提示去按照那些工具即可,而Android Studio的插件错误则是因为我的studio版本太低导致的,于是升级Android Studio到3.14版本,然后重新安装 Dart 和 Flutter插件即可。

关于Android Studio安装插件的流程:

  1. 打开Preferences,找到Plugins
  2. 点击下方的Browse Repositories
  3. 搜索flutter,然后点击install。因为flutter依赖于dart,所以会先安装dart。

安装完成之后重启Android Studio,在plugins里面直接搜索可以看见dart和flutter的状态。

错误Android license status unknown

碰到Android license 错误的时候,只需要执行

1
flutter doct--android - licenses

然后一直输入 y 即可。但执行该命令的时候确保在网络状况很好的情况下,这里有一个比较坑的地方,我在公司尝试了无数次发现不行。回到家百兆电信光纤一次就成功了。

当处理完这些错误之后,再次执行 flutter doctor,会发现已经没有错误了:

1
2
3
4
5
6
Doctor summary(to see all details, run flutter doctor - v): 
[✓] Flutter(Channel beta, v0 .5 .1, on Mac OS X 10.12 .6 16 G1036, locale zh - Hans - CN)
[✓] Android toolchain - develop for Android devices(Android SDK 25.0 .2)
[✓] iOS toolchain - develop for iOS devices(Xcode 9.2)
[✓] Android Studio(version 3.1)
[!] Connected devices!No devices available

No devices是说没有一个可用设备,这个我们可以忽略掉,后续把手机连上电脑,就不会有这个提示了。

至此,我们对于flutter的环境已经安装完成,下面开始愉快的撸码吧!