公司项目需要,要用 Flutter 开发 APP,原来的开发环境是Mac + Android Studio,下面记录搭建Flutter开发环境的流程。具体系统要求可以看: 在macOS上搭建Flutter开发环境,不过还是太复杂,Mac + Android Studio 的兄弟跟着下面的流程走就好了。
安装Flutter SDK
在Mac的Terminal输入指令:
git clone -b beta https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH
flutter doctor
执行 flutter doctor
后出现:
Failed to retrieve the Dart SDK from: https://storage.googleapis.com/flutter_infra/flutter/54ad777fd29b031b87c7a68a6637fb48c0932862/dart-sdk-darwin-x64.zip
If you're located in China, please see this page:
https://flutter.dev/community/china
已经科学上网了但是没有成功,打开上面的官方教程:Using Flutter in China ,配置环境变量:
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
flutter doctor
再次执行 flutter doctor
后成功。Flutter SDK目录在 /Users/用户名/flutter
下。
Android Studio 安装 Flutter 插件
需要两个插件,Flutter和Dart,打开Preferences -> Plugins -> 搜索并安装Flutter插件,安装时会自动安装Dart,安装完成后重启AS。
image.png重启后,File->New 中出现 New Flutter Project,说明安装成功。
image.png
File -> New -> New Flutter Project,选择Flutter Application -> Next,填写 Flutter SDK的路径。
image.png
Next -> 设置 Company domain,Finish,完成初始化需要等待几分钟。 然而事实是卡死在这一步了。
查看项目目录,发现项目已经创建成功了,强制退出AS,再重新打开这个项目,main.dart
文件里面提示导包错误:
error: Target of URI doesn't exist: 'package:flutter/material.dart'. (uri_does_not_exist at [flutter_app] lib/main.dart:1)
在Event Log里面提示要 Run flutter packages get
,点击之后重启AS,导包错误就不再提示了。
运行main.dart
,出现下面的提示:
Waiting for another flutter command to release the startup lock...
解决办法如下:
1、打开flutter的安装目录/bin/cache/
2、删除lockfile文件
3、重启AndroidStudio
再次运行 main.dart
,终于成功安装到手机,成功开启Flutter之旅。
网友评论