安装与运行

Windows 环境配置

  1. 首先设置两个环境变量
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
  1. 点击下载 最新可用的 flutter 安装包, 或者克隆:
git clone https://github.com/flutter/flutter.git
  1. 下载完成后,解压并将 bin 目录添加到 PATH 环境变量

创建项目

使用以下命令创建项目:

flutter create flutter_test

项目名称只能有小写字母及下划线

运行项目

运行前先检查是否有可用的模拟器:

$ flutter devices
1 connected device:
OPPO R11 • 127.0.0.1:62001 • android-x86 • Android 5.1.1 (API 22)

显示以上的信息说明有可用的设备

使用以下命令运行项目:

flutter run
flutter run --trace-startup --profile # 统计应用启动时间

注意以下热更新的提示:

To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

热更新输入 r,热重启输入 R

使用 VSCode 调试

  1. 在 VSCode 中安装 Dart 和 Flutter 插件
  2. 按 F5 键或调用 Debug>Start Debugging 运行调试
  3. 变更 lib/main.dart 文件,将可以看到热更新效果

使用 Android Studio 调试

  1. 安装 Flutter 和 Dart 插件
  2. 可以看到工具栏有相关的调试选项

检测问题

如果不能够正常运行, 可以通过以下命令检测问题:

$ flutter doctor --verbose
[] Flutter (Channel stable, v1.7.8+hotfix.4, on Microsoft Windows [Version 10.0.17134.950], locale zh-CN)
    • Flutter version 1.7.8+hotfix.4 at D:\Software\flutter
    • Framework revision 20e59316b8 (5 weeks ago), 2019-07-18 20:04:33 -0700
    • Engine revision fee001c93f
    • Dart version 2.4.0
[] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at D:/Software/android-sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-Q, build-tools 28.0.3
    • ANDROID_HOME = D:/Software/android-sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.
[] Android Studio (version 3.4)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 38.1.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
[] IntelliJ IDEA Ultimate Edition (version 2018.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4
    • Flutter plugin version 38.1.1
    • Dart plugin version 183.6270
[] VS Code (version 1.37.1)
    • VS Code at C:\Users\quanzaiyu\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.3.0
[] Connected device (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.0 (API 24) (emulator)
• No issues found!

错误解决

安装 Dart 的时候提示:

Downloading Dart SDK from Flutter engine 9bfa4f53cdbec57b0c3badc47bc13da145816c8d...
Unknown operating system. Cannot install Dart SDK.

解决方案: 到 flutter 的 bin 目录下执行

flutter.bat doctor

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress