[Flutter入门]:详细记录在Windows上搭建Flutter开发环境

一、使用镜像

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

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

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

二、获取Flutter SDK

  1. 去flutter官网下载其最新可用的安装包,点击下载 ;注意,Flutter的渠道版本会不停变动,请以Flutter官网为准。另外,在中国大陆地区,要想正常获取安装包列表或下载安装包,可能需要翻墙,读者也可以去Flutter github项目下去下载安装包 。
  2. 将安装包zip解压到你想安装Flutter SDK的路径(如:C:\src\flutter;注意,不要将flutter安装到需要一些高权限的路径如C:\Program Files\)。
  3. 在Flutter安装目录的flutter文件下找到flutter_console.bat,双击运行并启动flutter命令行,接下来,你就可以在Flutter命令行运行flutter命令了。

三、更新环境变量

要在终端运行 flutter 命令, 你需要添加以下环境变量到系统PATH:

  • 转到 “控制面板>用户帐户>用户帐户>更改我的环境变量”
  • 在“用户变量”下检查是否有名为“Path”的条目:
    • 如果该条目存在, 追加 flutter\bin的全路径,使用 ; 作为分隔符.
    • 如果条目不存在, 创建一个新用户变量 Path ,然后将 flutter\bin的全路径作为它的值.
  • 在“用户变量”下检查是否有名为”PUB_HOSTED_URL”和”FLUTTER_STORAGE_BASE_URL”的条目,如果没有,也添加它们。

重启Windows以应用此更改。

注意:我第一次是直接取github下载的安装包,解压到E盘的flutter文件夹下,配置好环境变量,然后在windows命令行下运行flutter doctor,结果命令行窗口闪退;运行其它flutter命令也是一样。

然后我用windows PowerShell运行flutter doctor,没有闪退,但可以看到错误信息,提示以下错误:

C:\Users\Star> flutter help
Error: The Flutter directory is not a clone of the GitHub project.
The flutter tool requires Git in order to operate properly;
to set up Flutter, run the following command:
git clone -b stable https://github.com/flutter/flutter.git

信息提示大概意思是我手动下载的flutterSDK不是github上克隆的,需要用git命令clone一份。我把手动下载的flutter文件删掉,然后执行以下命令:

E:\3rdComponents\flutter> git clone -b stable https://github.com/flutter/flutter.git
Cloning into 'flutter'…
remote: Enumerating objects: 315554, done. remote: Counting objects: 100% (116/116), done. remote: Compressing objects: 100% (94/94), done. remote: Total 315554 (delta 47), reused 63 (delta 21), pack-reused 315438 eceiving objects: 100% (315554/315554), 148.70 MiB
Receiving objects: 100% (315554/315554), 148.87 MiB | 582.00 KiB/s, done.
Resolving deltas: 100% (244607/244607), done.
Checking connectivity… done.
Checking out files: 100% (5200/5200), done.
PS E:\3rdComponents\flutter> flutter help
flutter : 无法将“flutter”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路
径正确,然后再试一次。
所在位置 行:1 字符: 1

根据以上提示,可以确定flutter SDK安装成功了,但系统尚无法识别flutter命令,这是因为环境变量还没有生效(我改变了flutterSDK的路径,更改了环境变量),重启Windows Power Shell后再次执行flutter help,成功运行。

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

 C:\Users\Star> flutter help
Checking Dart SDK version...
Downloading Dart SDK from Flutter engine ...
Building flutter tool...
Running pub upgrade...
Manage your Flutter app development.

Common commands:

  flutter create <output directory>
    Create a new Flutter project in the specified directory.

  flutter run [options]
    Run your Flutter application on an attached device or in an emulator.

Usage: flutter <command> [arguments]

Global options:
-h, --help                  Print this usage information.
-v, --verbose               Noisy logging, including all shell commands executed.
                            If used with --help, shows hidden options.
-d, --device-id             Target device id or name (prefixes allowed).
    --version               Reports the version of this tool.
    --suppress-analytics    Suppress analytics reporting when this command runs.

Available commands:
  analyze           Analyze the project's Dart code.
  assemble          Assemble and build Flutter resources.
  attach            Attach to a running app.
  bash-completion   Output command line shell completion setup scripts.
  build             Build an executable app or install bundle.
  channel           List or switch Flutter channels.
  clean             Delete the build/ and .dart_tool/ directories.
  config            Configure Flutter settings.
  create            Create a new Flutter project.
  devices           List all connected devices.
  doctor            Show information about the installed tooling.
  downgrade         Downgrade Flutter to the last active version for the current channel.
  drive             Run integration tests for the project on an attached device or emulator.
  emulators         List, launch and create emulators.
  format            Format one or more Dart files.
  gen-l10n          Generate localizations for the current project.
  install           Install a Flutter app on an attached device.
  logs              Show log output for running Flutter apps.
  precache          Populate the Flutter tool's cache of binary artifacts.
  pub               Commands for managing Flutter packages.
  run               Run your Flutter app on an attached device.
  screenshot        Take a screenshot from a connected device.
  symbolize         Symbolize a stack trace from an AOT-compiled Flutter app.
  test              Run Flutter unit tests for the current project.
  upgrade           Upgrade your copy of Flutter.

Run "flutter help <command>" for more information about a command.
Run "flutter help -v" for verbose help output, including less commonly used options.

  ╔════════════════════════════════════════════════════════════════════════════╗
  ║                 Welcome to Flutter! - https://flutter.dev                  ║
  ║                                                                            ║
  ║ The Flutter tool uses Google Analytics to anonymously report feature usage ║
  ║ statistics and basic crash reports. This data is used to help improve      ║
  ║ Flutter tools over time.                                                   ║
  ║                                                                            ║
  ║ Flutter tool analytics are not sent on the very first run. To disable      ║
  ║ reporting, type 'flutter config --no-analytics'. To display the current    ║
  ║ setting, type 'flutter config'. If you opt out of analytics, an opt-out    ║
  ║ event will be sent, and then no further information will be sent by the    ║
  ║ Flutter tool.                                                              ║
  ║                                                                            ║
  ║ By downloading the Flutter SDK, you agree to the Google Terms of Service.  ║
  ║ Note: The Google Privacy Policy describes how data is handled in this      ║
  ║ service.                                                                   ║
  ║                                                                            ║
  ║ Moreover, Flutter includes the Dart SDK, which may send usage metrics and  ║
  ║ crash reports to Google.                                                   ║
  ║                                                                            ║
  ║ Read about data we send with crash reports:                                ║
  ║ https://flutter.dev/docs/reference/crash-reporting                         ║
  ║                                                                            ║
  ║ See Google's privacy policy:                                               ║
  ║ https://policies.google.com/privacy                                        ║
  ╚════════════════════════════════════════════════════════════════════════════╝
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容