Flutter环境搭建(MAC)

2018-12-17  本文已影响118人  lltree

谨以此文献给想少走弯路的人,笔者参考传送门走了不少弯路

1 配置环境变量

由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用户环境变量中:

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

注意: 此镜像为临时镜像,并不能保证一直可用,读者可以参考详情请参考 Using Flutter in China 以获得有关镜像服务器的最新动态。

2 获取Flutter SDK

首先确定你想把SDK安装到哪个目录,在该目录下创建development目录,名字可以随便取。但是为了跟各方面统一,建议使用名字development

test:~ 用户名$ pwd
/Users/[用户名]
test:~ [用户名]$ mkdir development

获取Flutter SDK方法一:

去flutter官网下载其最新可用的安装包,转到下载页
注意,Flutter的渠道版本会不停变动,请以Flutter官网为准。另外,在中国大陆地区,要想正常获取安装包列表或下载安装包,可能需要翻墙,读者也可以去Flutter github项目下去下载安装包,转到下载页

把下载的安装包放到该目录中,然后使用下边命令解压

unzip ~/Downloads/flutter_macos_v0.5.1-beta.zip

获取Flutter SDK方法二:

使用git操作

git clone https://github.com/flutter/flutter.git

3 添加flutter命令相关工具到path中:

export PATH=`pwd`/flutter/bin:$PATH

注意:此代码只能暂时针对当前命令行窗口设置PATH环境变量,所以暂时不要关闭当前命令行窗口

添加永久环境变量

  • 1 确定您Flutter SDK的目录,您将在步骤4中用到。
  • 2 打开(或创建) $HOME/.bash_profile. 文件路径和文件名可能在您的机器上不同.
  • 3 添加以下行并更改。
export PUB_HOSTED_URL=https://pub.flutter-io.cn 
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn 
export PATH=/Users/[你的用户名,注意修改]/development/flutter/bin:$PATH
  • 4 运行命令 source $HOME/.bash_profile 刷新当前终端窗口
  • 5 随便再打开一个命令行窗口运行flutter命令检查,环境变量是否为全局永久的.有输出说明生效

例如:

 flutter -h

4 运行 flutter doctor检查依赖环境

flutter doctor

第一次运行一个flutter命令(如flutter doctor)时,它会下载它自己的依赖项并自行编译。以后再运行就会快得多。

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

例如:

gcl:~ [用户名]$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v1.1.1-pre.2, on Mac OS X 10.13.4 17E199, locale
    zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    ✗ Android licenses not accepted.  To resolve this, run: flutter doctor
      --android-licenses
[!] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    ✗ Verify that all connected devices have been paired with this computer in
      Xcode.
      If all devices have been paired, libimobiledevice and ideviceinstaller may
      require updating.
      To update with Brew, run:
        brew update
        brew uninstall --ignore-dependencies libimobiledevice
        brew uninstall --ignore-dependencies usbmuxd
        brew install --HEAD usbmuxd
        brew unlink usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ! CocoaPods out of date (1.5.0 is recommended).
        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 upgrade:
        brew upgrade cocoapods
        pod setup
[✓] Android Studio (version 3.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.30.0)
[✓] Connected device (1 available)

! Doctor found issues in 2 categories.

5 安装调试命令

1 安装 homebrew (如果已经安装了brew,跳过此步骤)

2 打开终端并运行这些命令来安装用于将Flutter应用安装到iOS设备的工具

brew update
brew install --HEAD libimobiledevice
brew install ideviceinstaller ios-deploy cocoapods
pod setup

6 安装Flutter的编辑器

目前Flutter支持的编辑器分别为Android Studio、IntelliJ或VS Code。
MAC下我还是选择VS Code。

6.1 Visual Studio Code (VS Code) 安装
6.2 安装Flutter插件

1 启动 VS Code
2 调用 View>Command Palette…
3 输入 ‘install’, 然后选择 Extensions: Install Extension action
4 在搜索框输入 flutter , 在搜索结果列表中选择 ‘Flutter’, 然后点击 Install
5 选择 ‘OK’ 重新启动 VS Code

上一篇下一篇

猜你喜欢

热点阅读