Mac - Flutter 開(kāi)發(fā)環(huán)境搭建

Flutter

Flutter中文官網(wǎng):https://flutterchina.club
Glutter中文官方安裝教程:https://flutterchina.club/get-started/install/

第一步

一. 檢查配置

參考:https://flutterchina.club/setup-macos/

要安裝并運(yùn)行Flutter,您的開(kāi)發(fā)環(huán)境必須滿足以下最低要求:
操作系統(tǒng): macOS (64-bit) macOS Catalina 10.15.3(及以上)
磁盤(pán)空間: 700 MB (不包括Xcode或Android Studio的磁盤(pán)空間)

工具: Flutter 依賴下面這些命令行工具.

  1. Xcode 11.3.1(及以上)
  2. bash, mkdir, rm, git, curl, unzip, which

第二步

下載SDK

  1. 使用鏡像
    參考:https://flutter.dev/community/china
    執(zhí)行以下命令將Flutterclone到想要存放Flutter的位置
  2. 使用Git管理FlutterSDK,Flutter To GitHub
$ export PUB_HOSTED_URL=https://pub.flutter-io.cn
$ export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
$ git clone -b dev https://github.com/flutter/flutter.git
$ export PATH="$PWD/flutter/bin:$PATH"
$ cd ./flutter
$ flutter doctor

下載并安裝Flutter SDK

Flutter SDK下載
注意,F(xiàn)lutter的渠道版本會(huì)不停變動(dòng),請(qǐng)以Flutter官網(wǎng)為準(zhǔn)。另外,在中國(guó)大陸地區(qū),要想正常獲取安裝包列表或下載安裝包,可能需要翻墻,讀者也可以去Flutter github項(xiàng)目下去下載安裝包,轉(zhuǎn)到下載頁(yè)。

Mac電腦下載后通常會(huì)自動(dòng)解壓為文件夾,
若未自動(dòng)解壓可解壓安裝包到你想安裝的目錄,如:

cd ~/development
unzip ~/Downloads/flutter.zip

添加flutter相關(guān)工具到path中:

第三步

編輯器

如果有Android Studio 或者 VSCode即可,沒(méi)有的可以前往下載
Visual Studio Code :https://code.visualstudio.com

配置環(huán)境變量

找到用戶文件夾,shift + command + . 顯示隱藏文件 右鍵 以文本編輯的形式打開(kāi)

添加環(huán)境變量
參考:官方文檔(https://flutterchina.club/setup-macos/) - 更新環(huán)境變量
打開(kāi)終端 輸入:open -e .bash_profile

注:若未打開(kāi)文件或提示文件未找到則,打開(kāi)文本編輯,新建命名為.bash_profile的文件并保存在用戶目錄下,并回到終端繼續(xù)輸入 open -e .bash_profile
路徑:/Users/***/.bash_profile

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=PATH_TO_FLUTTER_GIT_DIRECTORY/flutter/bin:$PATH

注意:PATH_TO_FLUTTER_GIT_DIRECTORY 為你flutter的路徑,比如“~/document/code”
示例:.bash_profile

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

export FLUTTER_HOME=/Users/ming/Documents/flutter
export PATH=$PATH:$FLUTTER_HOME/bin

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

打開(kāi)終端 運(yùn)行 source $HOME/.bash_profile 讓配置文件立即生效

運(yùn)行 echo $PATH 檢查配置文件,存在剛添加的則配置成功,

終端若使用的是 -zsh,會(huì)有一個(gè)坑點(diǎn),運(yùn)行 echo $PATH 檢查配置文件會(huì)找不到新加的配置,按照上面步驟打開(kāi).zshrc文件 在其中添加:source ~/.bash_profile

一切正常示例:
運(yùn)行 flutter doctor檢查環(huán)境,? 表示配置成功,? 表示未成功

ming@bogon ~ % flutter doctor            
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, 2.0.1, on macOS 11.3 20E5186d darwin-x64, locale
    zh-Hans-CN)
[?] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[?] Xcode - develop for iOS and macOS
[?] Chrome - develop for the web
[?] Android Studio (version 4.1)
[?] Connected device (2 available)

? No issues found!

完成

完成以上操作Flutter 開(kāi)發(fā)環(huán)境基本搭建完畢

Flutter更新

要同時(shí)更新Flutter SDK和你的依賴包,在你的應(yīng)用程序根目錄(包含pubspec.yaml文件的目錄)中運(yùn)行flutter upgrade 命令:

flutter upgrade
如遇到
在執(zhí)行 flutter upgrade 時(shí)出現(xiàn) fatal: unable to access 'https://github.com
參照:http://m.itdecent.cn/p/72e21df41ac9,進(jìn)行解決

遇到錯(cuò)誤

work@bogon ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, 2.0.5, on macOS 11.2.3 20D91 darwin-x64, 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
    ? CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin
        code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see
      https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.
[?] Chrome - develop for the web (Cannot find Chrome executable at
    /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (not installed)
[?] VS Code (version 1.55.2)
[!] Connected device
    ! No devices available

解決方案

  1. Pod 沒(méi)有安裝,參照: 進(jìn)行安裝
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容