上周任务不多,闲来无事想学习一下flutter耍一耍,发现flutter的环境搭建步骤还是很繁琐的,官网的搭建教程只是按步骤让你进行操作,中间出现的问题完全没有提及,对我这种没搞过原生开发的小白来说超级不友好。而网上很多相关博客教程,感觉不够详细,许多环境搭建过程中的坑确实是提到了,但解决的办法写的比较笼统,在此我将本次环境搭建锁遇到的各种奇奇怪怪的问题一一汇总,争取结合各路大神的博客加上自己实际遇到的问题整理出一篇超级实用且详细的教程,让童鞋们在学习flutter的路上少走弯路,让你的起跑顺畅起来,大神们请指正或忽略。
Flutter是相对新出的框架,对系统有一定的要求。
两种方式:官网下载和github下载
将下载好的sdk保存后解压。
cd ~/development unzip ~/Downloads/flutter_macos_v1.5.4-hotfix.2-stable.zip
由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,可以将如下环境变量加入到用户环境变量中。
如何更改环境变量?
echo $PATH
,按回车执行命令查看当前变量值。
sudo vi ~/.bash_profile
,按回车输入密码后用vi打开用户目录下的bash_profile文件。一定要用sudo,否则没权限保存文件。
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”,千万别顺手全粘贴上去,考试连姓名都抄的同学不是一个合格的学渣。
source ~/.bash_profile
或重启命令行刷新当前终端窗口。
echo $PATH
验证目录是否在已经在PATH中,正确的输出如下,每个电脑输出可能不一样,但当你看到flutter路径出现即说明配置成功。
/mySpace/flutterSDK/flutter/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/mongodb:/usr/local/go/bin~
到此我们已经安装完flutter了,但此时还不具备开发的能力,flutter运行需要很多插件,运行以下命令查看是否需要安装其它依赖项来完成安装:
flutter doctor
这时候它会将你未安装的依赖一一列出,每个电脑缺少的依赖都不尽相同,例如下面这样:
✗ Android toolchain - develop for Android devices
• Android SDK at /Users/obiwan/Library/Android/sdk
✗ Android SDK is missing command line tools; download from https://goo.gl/XxQghQ
• Try re-installing or updating your Android SDK,
visit https://flutter.dev/setup/#android-setup for detailed instructions.
To install, run:
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
先搞定需要brew安装的,如果你英文好,按着提示一条一条的安装,如果英文不好的话,那你可以找关键词,比如上面的输出,可以从中找到 brew install
字眼,将所有需要brew安装的统统安装上即可。还是以上面为例,可以看到brew相关的有:
brew install --HEAD libimobiledevice
brew install ideviceinstaller
brew install ios-deploy
brew install cocoapods
执行上面命令逐条安装依赖。再次运行flutter doctor, 这个时候需要安装的一些插件错误已经不会报了,但还是会有些其它的常见问题将会在下面一一列举出来。
Cocoapods is installed but not initialized
Cocoapods已经安装但没有初始化,执行下面语句完成初始化(可能较慢,耐心等待):
pod setup
brew install --HEAD libimobiledevice
命令会抛出如下异常:
configure: error: Package requirements (libusbmuxd >= 1.1.0) were not met: Requested 'libusbmuxd >= 1.1.0' but version of libusbmuxd is 1.0.10 Consider adjusting the PKG_CONFIG_PATH environment variable if you installed software in a non-standard prefix. Alternatively, you may set the environment variables libusbmuxd_CFLAGS and libusbmuxd_LIBS to avoid the need to call pkg-config. See the pkg-config man page for more details. READ THIS: https://docs.brew.sh/Troubleshooting
这时候需要运行brew unlink usbmuxd & brew install --HEAD usbmuxd
而不是libusbmuxd
。
macOS支持为iOS和Android开发Flutter应用程序。现在完成两个平台设置步骤中的至少一个,以便能够构建并运行你的第一个Flutter应用程序
至此,全部环境搭建步骤结束,不敢保证全部正确,但我尽可能的把我的安装过程和问题都列了出来,希望可以帮到各位同学,祝大家起跑顺利,若果安装过程中有任何问题,大家可以评论或在我的公众号前端小苑留言,看到后我会第一时间回复。