安装Dart
brew tap dart-lang/dart
brew install dart
# ~/.zshrc 或 ~/.bashrc 新增1行:
export PATH="$HOME/.pub-cache/bin:$PATH" # pub
安装fvm (同时安装多个flutter版本)
pub global activate fvm
fvm install stable # 或指定比如: 1.10.7, [Versions](http://url.cn/52OcNkr)
# ~/.zshrc 或 ~/.bashrc 新增1行:
export PATH="$HOME/fvm/versions/stable/bin:$PATH" # fullter
# fvm list # 已安装的版本号
flutter version manager
fvm
或者flutter channel
切换版本都不太好用(一个是需要以fvm开始命令, 一个是不能按项目切换版本),
推荐使用 direnv
设定:
brew install direnv
# 项目目录内执行 (指定了version为stable):
direnv allow;
echo 'VERSION="stable"; export PATH="$HOME/fvm/versions/$VERSION/bin:$PATH"' > .envrc;
信息打印
flutter --version
flutter doctor -v # 检查环境配置
# 安装依赖, 比如 XCode, Android Studio
brew install cocoapods
编辑器设定:
VSCode
- Extensions安装:Flutter, Dart
- 如果 VSCode 提示: Could not find a flutter SDK: 重启下VSCode
quicktype插件, 根据JSON生成Data Class. 用法:
- VSCode > Settings 设定
"quicktype.justTypes": false
- 创建并打开
xxx.dart
, 复制json代码, VSCode:view > Command Palette > Paste JSON as Code
, 根据剪贴板的json生成Code. - 生成的code, 最好正则替换下:
\b(json\[.*\]).map\(
>($1 ?? []).map(
,fromJson\((json\[.*\])\)
>fromJson($1 ?? {})
- VSCode > Settings 设定
Android Studio
- Android Studio(启动并完成设置向导, Configure/Plugins安装: Flutter, Dart)
- shell执行:
flutter doctor --android-licenses
Perferences:
- Flutter Sdk path:
<user>/fvm/versions/stable
- Dart Sdk path:
<user>/fvm/versions/stable/bin/cache/dart-sdk
- Flutter Sdk path:
模拟器
Android模拟器, Android Studio配置:
- Configure/Tools - AVD Manager, Create Virtual Device, 选择一个设备, Next.
- 其他版本Android: Preferences - Appearance & Behavior - System Settings - Android SDK, 勾选版本(比如: Android 8.0)安装.
IOS模拟器, Xcode配置:
- Open Developer Tool - Simulator, Hardware - Device, 选择iOS和iPhone型号.
- 其他版本iOS: Perferences - Components, Simulator 选择一个iOS版本下载.
flutter emulators # 列出模拟器
# flutter emulators --launch apple_ios_simulator # 启动模拟器
环境搭建完毕, 推荐用VSCore开发.