在 macOS 上搭建 Flutter 開發環境
阿新 • • 發佈:2020-12-21
下載 Flutter SDK
flutter官網下載:https://flutter.io/sdk-archive/#macos
若上述連結無法訪問,可通過GitHub下載
將下載的 flutter sdk 解壓到一個地方(這裡解壓完成後的目錄地址之後要用到),例如我直接放置在當前使用者的目錄下
配置 Flutter 環境變數及映象
- 開啟終端工具,使用 vim 進行環境配置,輸入如下命令
vim ~/.bash_profile
- 在開啟的檔案中新增如下路徑
export PATH=/Users/使用者名稱/flutter/bin:$PATH // PATH=/Users/使用者名稱/... 這裡是指上面你解壓的sdk的位置資訊 // 我這裡的位置資訊是:export PATH=/Users/y***i/flutter/bin:$PATH
- 由於在國內訪問Flutter可能會受到限制,Flutter官方為中國開發者搭建了臨時映象,將如下環境變數加入到使用者環境變數中:
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
- 至此,環境變數及映象都配置好了,再執行如下命令讓它生效
source ~/.bash_profile
- 使用如下命令檢查一下是否安裝成功了
flutter -h
出現如下可用的 flutter 命令提示後就是安裝成功了
Common commands: flutter create <output directory> Create a new Flutter project in the specified directory. flutter run [options] Run your Flutter application on an attached device or in an emulator. Usage: flutter <command> [arguments] Global options: -h, --help Print this usage information. -v, --verbose Noisy logging, including all shell commands executed. If used with --help, shows hidden options. -d, --device-id Target device id or name (prefixes allowed). --version Reports the version of this tool. --suppress-analytics Suppress analytics reporting when this command runs. --packages Path to your ".packages" file. (required, since the current directory does not contain a ".packages" file)
檢查開發環境
在上面的步驟中,我們成功的安裝了 Flutter ,但還不具備開發環境。在開發中還需要很多軟體和外掛的支援,我們需要通過如下命令來檢視到底還需要哪些軟體和外掛
flutter doctor
會顯示如下資訊:
這裡顯示 ❌ 的就是必須要安裝的,帶❗️的則不是必須的。
首先,讓我們來解決 Android SDK 的問題,這裡直接下載安裝 Android Studio
-
安裝完成後,開啟 Android Studio ,點選右下角的 Configure --> Plugins --> 下載 Dart 和 Flutter
-
再執行
flutter doctor
檢視(這裡有個問題是我已經安裝了Dart和Flutter外掛,卻提示我沒有安裝,具體解決辦法可見:
-
然後解決Android toolchain下面的問題,只要執行
flutter doctor --android-licenses
命令,一直輸入 y 同意即可,這時候關於Android Studio及Android SDK的問題就解決了。
可以看到,我們還需要解決XCode的安裝問題