MacBook M1 Flutter環境搭建的實現步驟

最近入手瞭Apple M1,MacBook Air,由於之前未使用蘋果系列產品,並且Flutter官方和各項配套的軟件環境也還沒有成熟,導致搭建環境時碰到瞭不少坑,這裡總結記錄一波,來看文檔的同學,有不懂的地方直接發評論或者消息就好

一、基礎環境搭建

 git:

我是裝完homebrew,git就裝好瞭,homebrew的安裝方法按照這個來的:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

但是後面發現這個安裝方法會有一些問題,不過不影響我們使用Flutter,不放心的同學可以去找其他方法安裝git 

Flutter SDK:

國內的網絡環境下,先配置兩個環境變量,最好是~/.zshrc和~/.bash_profile兩個文件都配上,配完source一下

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

下載安裝直接按照官方文檔來:https://flutter.cn/docs/get-started/install/macos

執行命令 flutter precache 或者 flutter doctor時會報錯,因為沒有在命令行執行安裝rosetta的命令,詳細的錯誤信息可以去下面的github地址查看

還少瞭一步操作,我第一天的晚上就卡在這兒瞭,最後去github提瞭issue,得到瞭一位老哥的提點

https://github.com/flutter/flutter/issues/74511#issue-792217747

終端執行命令

/usr/sbin/softwareupdate --install-rosetta --agree-to-license

隨後再執行flutter命令正常,繼續按著官方的安裝文檔走,環境變量一定要配,不然以後在終端無法直接使用flutter命令

CocoaPods:

Flutter官方文檔給出的安裝方式是命令行運行命令

sudo gem install cocoapods

但是這樣後期跑帶插件的ios應用,需要用到Cocoapods時就會報錯

### Error
 
LoadError - dlsym(0x7ff8aa0382f0, Init_ffi_c): symbol not found - /Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/lib/ffi_c.bundle
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/lib/ffi.rb:6:in `rescue in <top (required)>'
/Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/lib/ffi.rb:3:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Library/Ruby/Gems/2.6.0/gems/ethon-0.12.0/lib/ethon.rb:2:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Library/Ruby/Gems/2.6.0/gems/typhoeus-1.4.0/lib/typhoeus.rb:2:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/sources_manager.rb:74:in `cdn_url?'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/sources_manager.rb:36:in `create_source_with_url'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/sources_manager.rb:21:in `find_or_create_source_with_url'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:178:in `block in sources'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:177:in `map'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:177:in `sources'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:1073:in `block in resolve_dependencies'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/user_interface.rb:64:in `section'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:1072:in `resolve_dependencies'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer/analyzer.rb:124:in `analyze'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer.rb:414:in `analyze'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer.rb:239:in `block in resolve_dependencies'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/user_interface.rb:64:in `section'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer.rb:238:in `resolve_dependencies'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/installer.rb:160:in `install!'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/command/install.rb:52:in `run'
/Library/Ruby/Gems/2.6.0/gems/claide-1.0.3/lib/claide/command.rb:334:in `run'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/lib/cocoapods/command.rb:52:in `run'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.0/bin/pod:55:in `<top (required)>'
/usr/local/bin/pod:23:in `load'
/usr/local/bin/pod:23:in `<main>'
――― TEMPLATE END ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
[!] Oh no, an error occurred.
Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=dlsym%280x7ff8aa0382f0%2C+Init_ffi_c%29%3A+symbol+not+found+-+%2FLibrary%2FRuby%2FGems%2F2.6.0%2Fgems%2Fffi-1.13.1%2Flib%2Fffi_c.bundle&type=Issues
If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new
Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md
Don't forget to anonymize any private data!
 
Looking for related issues on cocoapods/cocoapods...
Found no similar issues. To create a new issue, please visit:
https://github.com/cocoapods/cocoapods/issues/new

需要使用 Rosetta 方式運行終端執行命令才行

打開 訪達 -> 應用程序 -> 實用工具 -> 終端 -> 右鍵 -> 顯示簡介 -> 選中使用 Rosetta 打開

然後重新啟動終端,執行

sudo gem install cocoapods
sudo gem install ffi

二、安裝IDE

我安裝瞭IDEA,Xcode和AndroidStudio

IDEA:

個人習慣使用IDEA做開發,社區版即可,而且已經對M1版本做瞭適配(題外話,mac體驗果然好,打開IDEA的速度簡直起飛)

下載地址:https://www.jetbrains.com/idea/download/

安裝這裡說個笑話,因為沒用過mac,安裝都不會,後面才知道安裝是點擊安裝文件之後在出現的界面裡把程序拖到Applications裡😭

使用IDEA開發Flutter需要安裝Dart和Flutter插件

Xcode:

ios開發必備(即使你不用ta來寫代碼),我安裝隻是讓ta用來配置各項環境,省心(工具界的工具人?),裝完一定記得要運行一次

官方下載地址:appstore搜索安裝即可,比較大,時間會長一點

安裝完之後運行命令,做IOS開發的相關設置

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
sudo xcodebuild -license

AndroidStudio:

官方下載地址:https://redirector.gvt1.com/edgedl/android/studio/install/4.1.2.0/android-studio-ide-201.7042882-mac.dmg

裝這個隻是為瞭讓他把安卓的環境都準備好(又一個工具界的工具人),跟著安裝步驟一直點就好瞭,不過M1芯片的現在還沒法直接用AS附帶的安卓模擬器,真機調試把,windows下用過AS的老哥應該都很熟瞭

使用AS開發Flutter需要安裝Dart和Flutter插件

三、跑一個app試試

按照官方文檔來就行,隨便用一個第三方依賴包,可以檢測出CocoaPods是否正常安裝

這個是我之前自己在windows上寫的一個小應用,隻用瞭 shared_preferences這個包

運行flutter pub get獲取依賴包

開啟IOS模擬器,運行

最終效果: 

到此這篇關於MacBook M1 Flutter環境搭建的實現步驟的文章就介紹到這瞭,更多相關M1 Flutter環境搭建內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: