Flutter开发环境搭建-Mac

前阵子动手配置了Flutter的开发环境,把搭建过程记录了下来,比较简单,希望对新手有帮助。

搭建步骤如下(已具备Android Studio的开发环境):

1、iOS下载Xcode 提前配置好pod环境即可、Android 下载Android Studio,同时下载安装好Flutter和Dart的插件,然后下载Flutter的SDK。
推荐去官网下载,速度并不慢,网址:https://flutter.io/setup-macos/

2、解压zip,放在你需要放的路径下就行,配置环境变量时需要该路径。同时在AS中打开Preferences,搜索Dart和Flutter,配置好Dart SDK的路径,在解压的flutter/bin/cache/dart-sdk中。

image
image
image

升级flutter sdk版本:下载mac os最新版本,解压,修改环境变量并更换,同上2的步骤。
遇到警告:

Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.
JoJodeMBP:flutter_app zhoujuan$  flutter packages upgrade 
Warning! The 'flutter' tool you are currently running is from a different Flutter repository than the one last used by this package. The repository from which the 'flutter' tool is currently
executing will be used instead.
  running Flutter tool: /Users/zhoujuan/AndroidDevelop/flutter-enviroment/flutter
  previous reference  : /Users/zhoujuan/Downloads/Android-develop/flutter
This can happen when you have multiple copies of flutter installed. Please check your system path to verify that you are running the expected version (run 'flutter --version' to see which
flutter is on your path).

解决:flutter packages upgrade 更新一下即可。注意更新前需要先运行which flutter查看flutter path路径是否正确,运行flutter --version 查看当前使用版本是否正确。

3、在终端打开open -e .bash_profile ,配置环境变量:

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=${PATH}:解压后的flutter的路径/flutter/bin:$PATH

更换环境变量:

source .bash_profile  或者 . ~/.bash_profile

4、运行 flutter doctor,查看是否需要安装其它依赖项来完成安装:具体可以参考官网:https://flutterchina.club/setup-macos/

该命令检查您的环境并在终端窗口中显示报告。Dart SDK已经在捆绑在Flutter里了,没有必要单独安装Dart。 仔细检查命令行输出以获取可能需要安装的其他软件或进一步需要执行的任务(以粗体显示)

image

按照上面提示,安装相关缺少的其他软件:
我做了下面的操作:
(1)运行 flutter doctor --android-licenses ,一路选择yes即可
(2)Android Studio中安装flutter插件,重启Android Studio。
再运行 flutter doctor检测如下,ios相关的东西,不安装也可以新建flutter项目了,同时可以在Android设备和iphone模拟器上跑。

image

(3)下面是处理ios相关的命令时的操作,过程中会报很多Error。解决办法:HomeBrew 安装工具遇到的权限相关的问题CocoaPods安装流程与使用

简单点说就是卸载了HomeBrew,然后重新安装,就可以执行相关的brew命令了。
在终端执行卸载命令:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

然后执行安装命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

上述操作可能还会报下面的错误,可参考homerew安装Connection refused即可解决。

curl: (7) Failed to connect to raw.githubusercontent.com port 443:Connection refused

image
! Doctor found issues in 1 category.
JoJodeMacBook-Pro:~ zhoujuan$ brew install --HEAD libimobiledevice
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
==> Installing dependencies for libimobiledevice: autoconf, automake, libtool, pkg-config, sphinx-doc, gdbm, openssl, readline, sqlite, python@2, libxml2, libplist, libtasn1, libusb and usbmuxd
==> Installing libimobiledevice dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.mojave.bottle.4.tar.gz
######################################################################## 100.0%
Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.
==> Downloading https://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
######################################################################## 100.0%
Error: An exception occurred within a child process:
  Errno::EPERM: Operation not permitted @ dir_s_mkdir - /usr/local/Cellar
JoJodeMacBook-Pro:~ zhoujuan$ brew install ideviceinstaller
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
==> Installing dependencies for ideviceinstaller: libplist, libtasn1, openssl, libusb, usbmuxd, libimobiledevice and libzip
==> Installing ideviceinstaller dependency: libplist
==> Downloading https://homebrew.bintray.com/bottles/libplist-2.0.0.mojave.bottle.tar.gz
######################################################################## 100.0%
Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.
==> Installing dependencies for libplist: pkg-config, sphinx-doc, gdbm, openssl, readline, sqlite, python@2 and cython
==> Installing libplist dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.mojave.bottle.tar.gz
######################################################################## 100.0%
Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.
==> Downloading https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
######################################################################## 100.0%
Error: An exception occurred within a child process:
  Errno::EPERM: Operation not permitted @ dir_s_mkdir - /usr/local/Cellar
JoJodeMacBook-Pro:~ zhoujuan$ brew install ios-deploy
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
==> Downloading https://homebrew.bintray.com/bottles/ios-deploy-1.9.3.mojave.bottle.tar.gz
######################################################################## 100.0%
Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.
==> Downloading https://github.com/ios-control/ios-deploy/archive/1.9.3.tar.gz
==> Downloading from https://codeload.github.com/ios-control/ios-deploy/tar.gz/1.9.3
######################################################################## 100.0%
==> Downloading https://github.com/ios-control/ios-deploy/commit/9b23447e.diff?full_index=1
######################################################################## 100.0%
==> Patching
==> Applying 9b23447e.diff
patching file ios-deploy.xcodeproj/project.pbxproj
Error: An exception occurred within a child process:
  Errno::EPERM: Operation not permitted @ dir_s_mkdir - /usr/local/Cellar
JoJodeMacBook-Pro:~ zhoujuan$ brew install cocoapods
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
==> Downloading https://homebrew.bintray.com/bottles/cocoapods-1.5.3.mojave.bottle.tar.gz
######################################################################## 100.0%
Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.
==> Downloading https://github.com/CocoaPods/CocoaPods/archive/1.5.3.tar.gz
==> Downloading from https://codeload.github.com/CocoaPods/CocoaPods/tar.gz/1.5.3
######################################################################## 100.0%pod setup
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
Error: An exception occurred within a child process:
  Errno::EPERM: Operation not permitted @ dir_s_mkdir - /usr/local/Cellar

安装过程中遇到的问题,基本就是输入flutter doctor检查时,缺少安装的一些软件,安装错误提示,输入对应的命令行安装即可,如果安装失败什么的,自行百度基本都能解决,下面贴出部分本人安装时的命令行执行过程:
Last login: Thu May 30 15:32:08 on ttys000
JoJodeMacBook-Pro:~ zhoujuan$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale
    zh-Hans-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
    ✗ libimobiledevice and ideviceinstaller are not installed. To install with
      Brew, run:
        brew update
        brew install --HEAD usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
[✓] Android Studio (version 3.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.
JoJodeMacBook-Pro:~ zhoujuan$ brew update
Already up-to-date.
JoJodeMacBook-Pro:~ zhoujuan$ brew install --HEAD usbmuxd
Warning: usbmuxd HEAD-60109fd_1 is already installed and up-to-date
To reinstall HEAD_1, run `brew reinstall usbmuxd`
JoJodeMacBook-Pro:~ zhoujuan$ brew link usbmuxd
Warning: Already linked: /usr/local/Cellar/usbmuxd/HEAD-60109fd_1
To relink: brew unlink usbmuxd && brew link usbmuxd
JoJodeMacBook-Pro:~ zhoujuan$ brew install --HEAD libimobiledevice
Updating Homebrew...
==> Cloning https://git.libimobiledevice.org/libimobiledevice.git
Updating /Users/zhoujuan/Library/Caches/Homebrew/libimobiledevice--git
==> Checking out branch master
Already on 'master'
Your branch is up to date with 'origin/master'.
HEAD is now at 4727a86 common: Update socket.c to match the one from libusbmuxd
==> ./autogen.sh
==> ./configure --disable-silent-rules --prefix=/usr/local/Cellar/libimobiledevi
==> make install
  /usr/local/Cellar/libimobiledevice/HEAD-4727a86_3: 67 files, 1MB, built in 56 seconds
Removing: /Users/zhoujuan/Library/Caches/Homebrew/libimobiledevice--1.2.0_3.mojave.bottle.tar.gz... (279.8KB)
JoJodeMacBook-Pro:~ zhoujuan$ brew install ideviceinstaller
Updating Homebrew...
Warning: ideviceinstaller 1.1.0_4 is already installed and up-to-date
To reinstall 1.1.0_4, run `brew reinstall ideviceinstaller`
JoJodeMacBook-Pro:~ zhoujuan$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
[✓] Android Studio (version 3.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
JoJodeMacBook-Pro:~ zhoujuan$

都安装成功后的截图:

image

安装完成后就可以打开Android Studio新建Flutter工程了,可以跨平台运行在Android设备和ios模拟器中:

image
image
image
image
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容