系统要求
要安装并运行Flutter,您的开发环境必须满足以下最低要求:
操作系统: macOS (64-bit)
磁盘空间: 700 MB (不包括Xcode或Android Studio的磁盘空间).
工具: Flutter 依赖下面这些命令行工具.
bash, mkdir, rm, git, curl, unzip, which
1、设置镜像
由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用户环境变量中:
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
2、安装Flutter SDK
// 安装目录,以development为例,没有自己创建一个development文件夹
cd ~/development
// 下载flutter
git clone https://github.com/flutter/flutter.git
下载完成之后,添加flutter相关工具到path中:
export PATH=`pwd`/flutter/bin:$PATH
编辑bash_profile,设置环境变量
vim ~/.bash_profile
export FLUTTER_ROOT=/*你的安装目录*/flutter
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=/*你的安装目录*/flutter/bin:$PATH
运行 flutter doctor
运行以下命令查看是否需要安装其它依赖项来完成安装:
flutter doctor
之后,可能会出现这个问题,flutter dart-sdk下载失败
Downloading Dart SDK from Flutter engine 4e54bc93ca9aaf2156fb06266c9fe509e8599a5f...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:05:00 --:--:-- 0
curl: (56) Proxy CONNECT aborted due to timeout
Failed to retrieve the Dart SDK from: https://mirrors.sjtug.sjtu.edu.cn/flutter_infra/flutter/4e54bc93ca9aaf2156fb06266c9fe509e8599a5f/dart-sdk-darwin-x64.zip
If you're located in China, please see this page:
https://flutter.io/community/china
解决办法:
1、下载dart-sdk:
// 这里的 4e54bc93ca9aaf2156fb06266c9fe509e8599a5f 要跟上面的错误提示显示的一样
wget https://mirrors.sjtug.sjtu.edu.cn/flutter_infra/flutter/4e54bc93ca9aaf2156fb06266c9fe509e8599a5f/dart-sdk-darwin-x64.zip
找到安装目录,我这里是~/development/flutter/,把dart-sdk-darwin-x64.zip放到bin/cache/下,请确保文件名正确。
2、打开/bin/internal/update_dart_sdk.sh文件,注释掉以下行
rm -rf -- "$DART_SDK_PATH"
mkdir -m 755 -p -- "$DART_SDK_PATH"
curl --continue-at - --location --output "$DART_SDK_ZIP" "$DART_SDK_URL" 2>&1 || {
echo
echo "Failed to retrieve the Dart SDK from: $DART_SDK_URL"
echo "If you're located in China, please see this page:"
echo " https://flutter.io/community/china"
echo
rm -f -- "$DART_SDK_ZIP"
exit 1
}
3、再执行flutter doctor,完成。(这里执行时间较长,耐心等待完成)
安装完成
创建flutter工程
打开终端,cd到你要创建的路径,输入命令行:flutter create -t module my_flutter
ok,创建完成!
现在使用Visual Studio打开,开始flutter编程之旅
ps:使用Visual Studio创建和使用终端创建flutter貌似在结构上会有些不同,上面是使用Visual Studio创建的,下面是使用终端创建的,以ios为例,结构是有些不同的:
参考链接:
Add Flutter to existing apps
https://www.imooc.com/article/68309
https://flutterchina.club/setup-macos/
https://www.jianshu.com/p/6d2a4f31a4c7