flutter填坑之旅(環(huán)境搭建篇--mac系統(tǒng))

上次配置過Mac到flutter環(huán)境,但是由于最近系統(tǒng)更新了,什么都沒了又得從新配置,發(fā)現(xiàn)自己竟然好多都忘記了,看來還是得把它記下來才行

在Mac上安裝并運(yùn)行Flutter 最低要求:

  • 操作系統(tǒng):macOS(64-bit)
  • 磁盤空間:700MB(不包括Xcode或者Android Studio的磁盤空間)
  • 工具:Flutter 依賴 這些命令行工具bash curl git 2.x mkdir rm unzip which (可以使用檢測一下沒有都安裝上免得后期出問題)
    mac安裝git可以參考http://www.lxweimin.com/p/7edb6b838a2e

一、設(shè)置 Flutter 鏡像

配置flutter國內(nèi)鏡像

// 直接到用戶目錄 下 運(yùn)行 open -e .bash_profile 在文件中增加如下
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

注意這個(gè)鏡像地址不能保證永遠(yuǎn)可以用,得隨時(shí)關(guān)注Useing Flutter in China這個(gè)官網(wǎng)

二、獲取Flutter SDK

2.1 下載flutter sdk

flutter官網(wǎng)獲取對應(yīng)得flutter sdk https://flutter.dev/docs/development/tools/sdk/releases?tab=macos

image

推薦最好下載 stable channel這個(gè)穩(wěn)定版本
image

2.2 解壓

## development 這個(gè)根據(jù)自己實(shí)際得目錄更改
$ cd ~/development
## 下面~/ 后得目錄和下載得flutter版本根據(jù)自己實(shí)際名稱更改
$ unzip ~/Downloads/flutter_macos_v1.2.1-stable.zip

具體操作:


taodeMacBook-Pro:~ tao$ cd ~/dev-flutter/
taodeMacBook-Pro:dev-flutter tao$ ls
android-sdk
taodeMacBook-Pro:dev-flutter tao$ unzip ~/Downloads/flutter_macos_v1.7.8+hotfix.4-stable.zip 
taodeMacBook-Pro:dev-flutter tao$ ls
android-sdk flutter
taodeMacBook-Pro:dev-flutter tao$ 
taodeMacBook-Pro:dev-flutter tao$ ls flutter/
AUTHORS         README.md       examples
CODE_OF_CONDUCT.md  analysis_options.yaml   flutter_console.bat
CONTRIBUTING.md     bin         flutter_root.iml
LICENSE         dartdoc_options.yaml    packages
PATENTS         dev         version
taodeMacBook-Pro:dev-flutter tao$ 

2.3 添加flutter相關(guān)工具到path中
 export PATH="$PATH:`pwd`/flutter/bin"

該命令只是一次性的,對當(dāng)前對terminal 設(shè)置里一個(gè)flutter的環(huán)境變量,等關(guān)閉里電腦下次又得重新設(shè)置,想要設(shè)置個(gè)永久性的還是要
將解壓得路徑配置到.bash_profile中去

輸入命令打開.bash_profile


taodeMacBook-Pro:dev-flutter tao$ open ~/.bash_profile
taodeMacBook-Pro:dev-flutter tao$ 

在最后增加如下內(nèi)容:


export PATH=/Users/tao/dev-flutter/flutter/bin:$PATH
export ANDROID_HOME="/Users/tao/Library/Android/sdk"
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cnexport PATH="/usr/local/opt/gettext/bin:$PATH"

其中export PATH=/Users/tao/dev-flutter/flutter/bin:$PATH 這個(gè)是配置的flutter
解壓的目錄(需要指定到flutter下面到bin目錄下)
其中export ANDROID_HOME="/Users/tao/Library/Android/sdk"配置里Android sdk 路徑

taodeMacBook-Pro:dev-flutter tao$ flutter doctor
  ╔════════════════════════════════════════════════════════════════════════════╗
  ║ A new version of Flutter is available!                                     ║
  ║                                                                            ║
  ║ To update to the latest version, run "flutter upgrade".                    ║
  ╚════════════════════════════════════════════════════════════════════════════╝


Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.15 19A602, locale
    zh-Hans-CN)
    ? Downloaded executables cannot execute on host.
      See https://github.com/flutter/flutter/issues/6207 for more information

 
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.1)
    ? 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:
        brew install cocoapods
        pod setup
[?] iOS tools - develop for iOS devices
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
    ! No devices available

! Doctor found issues in 3 categories.
taodeMacBook-Pro:dev-flutter tao$ 

運(yùn)行flutter doctor 進(jìn)行環(huán)境檢測,會發(fā)現(xiàn)用很多依賴缺失,根據(jù)提示一一安裝
但是還有一個(gè)提示我的mac和我下載的flutter好像不兼容

Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.15 19A602, locale
    zh-Hans-CN)
    ? Downloaded executables cannot execute on host.
      See https://github.com/flutter/flutter/issues/6207 for more information

直接運(yùn)行flutter upgrade 安裝個(gè)最新版本的就好了


taodeMacBook-Pro:dev-flutter tao$ flutter upgrade
Upgrading Flutter from /Users/tao/dev-flutter/flutter...
../../third_party/dart/runtime/bin/snapshot_utils.cc: 149: error: Failed to memory map snapshot: /Users/tao/dev-flutter/flutter/bin/cache/dart-sdk/bin/snapshots/kernel-service.dart.snapshot

version=2.4.0 (Wed Jun 19 11:53:45 2019 +0200) on "macos_x64"
thread=5891, isolate=(null)(0x0)

【注意??命令中請使用 sudo 否則有些會提示錯(cuò)誤】
像這個(gè) 執(zhí)行個(gè)flutter doctor都報(bào)錯(cuò)

taodeMacBook-Pro:repos tao$ flutter doctor
Failed to write the version file to the artifact cache: "FileSystemException:
Cannot open file, path = '/Users/tao/dev-flutter/flutter/version' (OS Error:
Permission denied, errno = 13)".
Please ensure you have permissions in the artifact cache directory.
Failed to write the version file

廢話少說繼續(xù)吧...

## 在執(zhí)行一次
taodeMacBook-Pro:dev-flutter tao$ sudo flutter doctor -v
   Woah! You appear to be trying to run flutter as root.
   We strongly recommend running the flutter tool without superuser privileges.
  /
??
Downloading Dart SDK from Flutter engine d004bcd4d619fc3574761d63d7cf7b7291332c79...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  121M  100  121M    0     0  4763k      0  0:00:26  0:00:26 --:--:-- 3986k
## 這里還會有很多下載,就不貼了
## 最后會顯示flutter及dart 版本信息和缺失的依賴
taodeMacBook-Pro:dev-flutter tao$ sudo flutter doctor
Password:
   Woah! You appear to be trying to run flutter as root.
   We strongly recommend running the flutter tool without superuser privileges.
  /
??
Downloading Dart SDK from Flutter engine b863200c37df4ed378042de11c4e9ff34e4e58c9...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  263M  100  263M    0     0  5236k      0  0:00:51  0:00:51 --:--:-- 5379k
Building flutter tool...
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15 19A602, locale
    zh-Hans-CN)
 
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.1)
    ? CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with
        is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods. For more info, see
        https://github.com/flutter/flutter/issues/14293.
      To re-install CocoaPods, run:
        sudo gem install cocoapods
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

看提示好像還差一個(gè)cocoapods 再手工裝一個(gè)

注意??Mac 中文件查找使用 which directoryName 而不建議使用 find ~/ -name 'directoryName'


sudo gem install cocoapods

但是會發(fā)現(xiàn)執(zhí)行了如上命令幾次依然提示通用錯(cuò)誤,解決方法是


taodeMacBook-Pro:dev-flutter tao$ cd ~/.cocoapods/repos
taodeMacBook-Pro:repos tao$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
fatal: 目標(biāo)路徑 'master' 已經(jīng)存在,并且不是一個(gè)空目錄。
taodeMacBook-Pro:repos tao$ ls
master
taodeMacBook-Pro:repos tao$ rm -rf master/


taodeMacBook-Pro:repos tao$ 
taodeMacBook-Pro:repos tao$ 
taodeMacBook-Pro:repos tao$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
正克隆到 'master'...

手動(dòng)刪除老版本,克隆新版本
這個(gè)方法最后證實(shí)是失敗的....


 brew reinstall libimobiledevice
 brew install --HEAD libimobiledevice
 brew upgrade cocoapods
 

全部都又執(zhí)行了一次,最后依然報(bào)錯(cuò)麻麻但,不曉得咋搞,ruby我東重新裝過了
【最后我去官網(wǎng)從新下載了個(gè)flutter_macos_v1.9.1+hotfix.5-stable.zip

image

然后解壓測試??了媽蛋垃圾問題(推測是Mac版本兼容問題,出錯(cuò)都版本是10.14.1 而目前最新都版本為10.15.1)


taodeMacBook-Pro:dev-flutter tao$ unzip ~/Downloads/flutter_macos_v1.9.1+hotfix.5-stable.zip 
taodeMacBook-Pro:dev-flutter tao$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, v1.9.1+hotfix.5, on Mac OS X 10.15.1 19B88, locale zh-Hans-CN)
 
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[?] Xcode - develop for iOS and macOS (Xcode 11.1)
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.

還有輸入flutter doctor 會提示

image

這個(gè)千萬別點(diǎn)擊 移到廢紙簍;那樣flutter運(yùn)行就會出問題了

三、IOS開發(fā)環(huán)境設(shè)置

3.1 Xcode 安裝

1、直接去Apple store 下載 對應(yīng)得Xcode 然后安裝這個(gè)沒有什么說的


image

點(diǎn)擊下載得圖標(biāo)


image

在啟動(dòng)臺里得右下角你會看見一個(gè)正在下載得xcode
image

安裝完成再安裝些必須的組件


image

2、配置Xcode 命令行工具以使用新安裝得Xcode版本


$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

注意/Applications/Xcode.app/Contents/Developer 對應(yīng)得是自己得Xcode 得安裝路徑

3、 打開Xcode 許可協(xié)議,執(zhí)行如下命令


$ sudo xcodebuild -license

3.2 設(shè)置iOS 模擬器

使用命令開啟iOS模擬器


$ sudo open -a Simulator

四、Android開發(fā)環(huán)境設(shè)置

官網(wǎng)下載 android studio :https://developer.android.google.cn/studio)

image

image

下載完成直接點(diǎn)著左邊得Android Studio 拖到右邊得Applications文件里去,他就會提示你安裝了
image

接下來就是下一步下一步直到安裝結(jié)束
image

安裝完成后記得配置安裝dart和flutter 插件
配置dart

配置dart

五、新建項(xiàng)目

控制臺執(zhí)行 flutter create proj_name


taodeMacBook-Pro:flutter-project tao$ flutter create my_first_app_test
  my_first_app_test/.idea/workspace.xml (created)
Running "flutter pub get" in my_first_app_test...                   3.1s
Wrote 65 files.

All done!
[?] Flutter is fully installed. (Channel stable, v1.9.1+hotfix.5, on Mac OS X
    10.15.1 19B88, locale zh-Hans-CN)
[?] Android toolchain - develop for Android devices is fully installed. (Android
    SDK version 29.0.2)
[?] Xcode - develop for iOS and macOS is fully installed. (Xcode 11.1)
[?] Android Studio is fully installed. (version 3.5)
[?] IntelliJ IDEA Ultimate Edition is fully installed. (version 2018.3.4)
[!] Connected device is not available.

Run "flutter doctor" for information about installing additional components.

In order to run your application, type:

  $ cd my_first_app_test
  $ flutter run

Your application code is in my_first_app_test/lib/main.dart.

然后flutter run直接來跑一哈

## 啟動(dòng)iOS模擬器
taodeMacBook-Pro:flutter-project tao$ open -a Simulator
taodeMacBook-Pro:flutter-project tao$ ls
my_app          my_first_app        my_first_app_test
taodeMacBook-Pro:flutter-project tao$ cd my_first_app_test/
taodeMacBook-Pro:my_first_app_test tao$ flutter run

模擬測試
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,333評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,491評論 3 416
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,263評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,946評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,708評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,186評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,409評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,939評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,774評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,976評論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,641評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,872評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,650評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,958評論 2 373

推薦閱讀更多精彩內(nèi)容