1、进入根目录
cd ~
2、创建.bash_profile
touch .bash_profile
3、编辑.bash_profile
open -e .bash_profile
4、设置环境变量,在输入.bash_profile
export PATH="$PATH:【flutter的bin目录】"
export PUB_HOSTED_URL=https://pub.flutter-io.cn //国内镜像
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn //国内镜像
找到你安装的flutter目录,找到flutter/bin目录,直接把bin文件夹目录替换【flutter的bin目录】 5、保存.bash_profile
command+s
6、更新
source .bash_profile
7、验证是否成功
flutter doctor
如果终端没有显示flutter命令不存在,而是显示了一大串内容,证明成功了,如
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, 2.10.2, on macOS 11.6 20G165 darwin-arm, locale
zh-Hans-CN)
[?] Android toolchain - develop for Android devices
? Unable to locate Android SDK.
Install Android Studio from:
https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK
components.
(or visit https://flutter.dev/docs/get-started/install/macos#android-setup
for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[!] Xcode - develop for iOS and macOS
? Xcode installation is incomplete; a full installation is necessary for iOS
development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
[?] Chrome - develop for the web
[!] Android Studio (not installed)
[?] VS Code (version 1.64.2)
[?] Connected device (1 available)
[?] HTTP Host Availability (the doctor check crashed)
? Due to an error, the doctor check did not complete. If the error message
below is not helpful, please let us know about this issue at
https://github.com/flutter/flutter/issues.
? HandshakeException: Connection terminated during handshake
! Doctor found issues in 4 categories.
8、每次打开都要首先在终端输入
source .bash_profile
然后flutter命令才能使用,比较麻烦可以通过
echo 'source ~/.bash_profile' >> ~/.zshrc
进行设置,然后每次打开终端就会自动执行刷新命令,不用再手动刷新,可以直接使用flutter命令了 9、如果环境变量设置错误,而又执行了
echo 'source ~/.bash_profile' >> ~/.zshrc
那么会导致终端很多命令(如cd等等)失效的 可以通过下面的命令临时恢复
PATH=/bin:/usr/bin:/usr/local/bin:${PATH}
export PATH
然后
cd ~
open -e .bash_profile
从新对环境变量进行设置,如果不要了就直接删除步骤4添加的命令,再执行命令5,再次打开很多命令(如cd等等)就能从新使用了
本文参考:https://www.jianshu.com/p/d401bfa2b44b
|