热门搜索 :
考研考公
您的当前位置:首页正文

Flutter 环境配置与问题详解

来源:东饰资讯网

系统要求

  • 有能访问外网的 VPN
  • macOS 64bit
  • Xcode 或者 Android Studio,或者两者都有
  • 命令行工具:bash, mkdir, rm, git, curl, unzip

下载SDK

  1. 下载最新的
  2. 把压缩包放到你喜欢的地方,比如,Documents 目录下
  3. 双击解压缩,或通过命令: unzip ~/Documents/下载下来的包-beta.zip

添加 flutter 命令

解压缩后,不要急着进入 flutter 文件夹,还停留在 Documents 文件目录下(或者执行 cd ~/Documents 切到这个目录),然后执行下面的命令:

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

有了这一步,可以避免这个问题: command not found: flutter

检查环境依赖

执行下面命令来检测本机环境依赖

$ flutter doctor

输出如下:

[✓] Flutter (Channel beta, v0.11.3, on Mac OS X 10.14 18A391, locale zh-Hans-CN)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: 
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.io/setup/#android-setup for detailed instructions).
      If Android SDK has been installed to a custom location, set $ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.

[!] iOS toolchain - develop for iOS devices (Xcode 9.4.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
    ✗ ios-deploy not installed. To install with Brew:
        brew install ios-deploy
[!] Android Studio (not installed)
[✓] VS Code (version 1.29.0)
[✓] Connected device (2 available)

显示叉号 的,表示相应的工具没有安装。其中比较重要的两大开发工具是:XcodeAndroid Statudio

如果这两个没有安装,需要根据你的开发者身份,安装对应的开发工具

部署 iOS 环境

  1. 首先需要安装 Xcode,最好是最新版本的
  2. 启动 Xcode,同意它的 license 等(适用于新装Xcode)
  3. Xcode command-line 指定要使用的 Xcode 版本:
$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
  1. 选择 iPhone 5s 或之后版本的模拟器,然后执行:
$ open -a Simulator
  1. 再执行下面的部署命令:
$ brew update
$ brew install --HEAD usbmuxd
$ brew link usbmuxd
$ brew install --HEAD libimobiledevice
$ brew install ideviceinstaller ios-deploy cocoapods
$ pod setup

运行 Flutter 项目

进入某个 flutter 项目目录

$ cd ~/Documents/flutter/examples/hello_world

执行命令,安装 hello_world 到模拟器:

$ flutter run

运行一会后,模拟器已经安装了hello_world,但并没有launch。也许会报错:

ProcessException: Process "/usr/bin/xcrun" exited abnormally:
"io.flutter.examples.hello-world": -1

An error was encountered processing the command (domain=FBSOpenApplicationServiceErrorDomain, code=1):
The request to open ""io.flutter.examples.hello-world"" failed.
The request was denied by service delegate (SBMainWorkspace) for reason: NotFound ("Application ""io.flutter.examples.hello-world"" is
unknown to FrontBoard").
Underlying error (domain=FBSOpenApplicationErrorDomain, code=4):
    The operation couldn’t be completed. Application ""io.flutter.examples.hello-world"" is unknown to FrontBoard.
    Application ""io.flutter.examples.hello-world"" is unknown to FrontBoard.
  Command: /usr/bin/xcrun simctl launch 4B7B9269-47FA-47B6-8096-17AB675A3E4F "io.flutter.examples.hello-world" --enable-dart-profiling
  --enable-checked-mode --observatory-port=0
Error launching application on iPhone 5s.

这里暴露出了 flutter 的另一个问题:

根据 $(PRODUCT_BUNDLE_IDENTIFIER) 不能成功的launch 模拟器

解决方法是:

  • Xcode打开 hello-world 项目
  • 找到 info.plist
  • 修改 Bundle identifier 的值为 io.flutter.examples.hello-world

再次执行 flutter run. 模拟器成功 launch.

部署 Android

  1. 下载安装 .
  2. 启动 Android Studio,执行 “Android Studio Setup Wizard”
  3. 安卓设备启用 Developer optionsUSB debugging. 这个文档有详细说明
  4. 把设备连接到电脑,执行命令 flutter devices
  5. 启动应用程序 flutter run

Android 模拟器

  1. Launch Android Studio ->Tools -> Android -> AVD Manager 并选择 Create Virtual Device.

  2. 选择一个设备并选择 Next

  3. 为你要模拟的 Android 版本选择一个或多个系统镜像,然后选择 Next. 建议使用 x86x86_64 镜像 .

  4. Android Virtual Device Manager中, 点击 Run 启动模拟器

  5. 运行 flutter run . 可以看到连接的设备名称是 Android SDK built for <x86or其他型号>

至此,Flutter 的环境部署就结束了。

Top