01-mac flutter環境搭建

mac flutter環境搭建

安裝國內鏡像

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

下載flutter

  1. 下載flutter
  • flutter官網下載地址
  • flutter github下載地址
  1. 進入你想放置flutter的目錄
cd ~/development
  1. 解壓下載文件,記住改成你本身下載的版本號,按Y確認便可
unzip ~/Downloads/flutter_macos_v0.5.1-beta.zip

  1. 將flutter添加到path中
export PATH=`pwd`/flutter/bin:$PATH

此時你會發現,你打開一個新的命令行窗口是,運行flutter doctor,會報flutter不存在的錯誤,是由於環境變量還沒全局生效android

運行flutter

  1. 運行flutter doctor

錯誤詳情git

Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    ✗ Android licenses not accepted.  To resolve this, run: flutter doctor
      --android-licenses
[✗] 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
    ✗ 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:
        sudo gem install cocoapods
[!] Android Studio (version 4.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] VS Code (version 1.52.0)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[!] Connected device
    ! No devices available

上面的錯誤信息是告訴你東西未安裝完,好比xcode,Android Studio,Android Studio的dart、fultter插件,CocoaPods,VS Code的flutter插件未安裝github

  1. xcode安裝
  • 在app store搜索xcode,安裝便可
  • 運行下面命令
  • 再次運行flutter doctor會發現關於xcode的錯誤消失了
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
sudo gem install cocoapods
  1. Android Studio安裝
  • 下載安裝Android Studio
  • 打開Android Studio中Preferences,選擇Plugins,輸入flutter與dart,install安裝便可

  1. VS Code中flutter插件安裝
  • 打開vscode,點擊擴展,輸入flutter,install安裝,安裝以後從新打開vscode便可

  1. 再次運行flutter doctor,關於Android Studio相關的錯誤可能仍是有,運行下面的命令
// 注意AndroidStudio版本號
ln -s ~/Library/Application\ Support/Google/AndroidStudio4.1/plugins ~/Library/Application\ Support/AndroidStudio4.1
  1. 再次運行flutter doctor,下面這樣就都正常了

環境變量配置

  • 查看flutter的環境配置路徑path
echo $PATH
// 能夠看到flutter的路徑爲
/Users/name/development/flutter/bin:

  • 打開環境配置文件
open ~/.bash_profile
  • 或者vim編輯
vim ~/.bash_profile
  • 若是在編輯bash_profile時發現文件不存在
// 建立bash_profile文件
touch .bash_profile
  • bash_profile文件存在時,添加下面內容
// 添加國內鏡像
export PUB_HOSTED_URL="https://pub.flutter-io.cn"
export FLUTTER_STORAGE_BASE_URL="https://storage.flutter-io.cn"
// 添加安裝路徑,上面獲取到的
export PATH="/Users/name/development/flutter/bin:$PATH"
  • 更新環境配置
source ~/.bash_profile
  • 重啓編輯器便可,會發如今

最終的bash_profile文件macos

// .bash_profile
# HomeBrew
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
export PUB_HOSTED_URL="https://pub.flutter-io.cn"
export FLUTTER_STORAGE_BASE_URL="https://storage.flutter-io.cn"
# name爲本身電腦的名稱
export PATH="/Users/name/development/flutter/bin:$PATH"
source ~/.bashrc
# HomeBrew END

github查看更多文章vim

相關文章
相關標籤/搜索