去年项目结束后, 好几个月没用 flutter 了,前天(2020-03-03) 看到 linux desktop (master 分支)的支持 flutter create 了, 所以立马更新测试, 执行

相关服务:新加坡大带宽服务器租用

flutter upgrade

半天无响应,有时候提示 SSL 错误 . 根据经验, 应该是国内特殊的网络问题 (近期是因为新加坡亚马逊的服务器被强,而github就在那边有部属,国内基本都解析到那边), 所以开个代理,使用. 正常更新. 但是到了下载 engine 组件部分,如果 继续使用国内的镜像也会有问题,要么关闭代理,要么停止使用国内镜像 (这是一个 dart 底层库的问题)

用 flutter master 分支就是这么折腾, 针对国内网络问题, flutter 官方把 引擎组件, pub库 都做了国内镜像可以解决下载问题, 参考

在中国网络环境下使用 Flutter​flutter.cn
05121476fe2f3f58ddbc7ce1638bda6a.png

这一方案 针对 stable 使用用户是基本ok 的, 因为 stable 分支 检测版本更新的周期很长, 比如这段时间也不会触发 github 被 Q 的问题. 如果用 master 分支,基本每次执行命令都会检测一次版本更新, 就会非常折腾 .

正常更新 flutter 后, 创建一个项目

flutter create linux_demo

然而又出问题了, 程序卡在了 flutter pub get 上, 这个不是已经用镜像了么, 很奇怪,查了几个小时终于找到根源 flutter 有个 BotDetector 对象,检测一些 dev 环境, 而 pub get 会调用这个检测程序,作死的是,这个检测程序里面 hard code 了一个检测 Azure 环境的服务 http://169.254.169.254/metadata/instance

cf40a8ff5a63d7a9bd8c6954455ca047.png

而这个服务器也是被 Q 的, 这样就卡了, 也不设计一个超时机制. 而且已经有人提出了类似的问题, flutter_tools 也给了解决方案

https://github.com/flutter/flutter/pull/50579​github.com

增加一个环境变量 BOT 设置 BOT=false 可以禁用以上次BotDetector 功能.

所以要么在环境变量中增加BOT=false 要么执行

BOT=false flutter pub get 
BOT=false flutter run 
等

最新的master 怎么用呢?

终极解决方案就是

全局 FQ 网络, 比如部署在路由器上的工具,很都公司都会有方案.

折衷方案

修改 fluttertools 中检测版本的程序 在 $FLUTTERHOME/packages/flutter_tools/src/lib/version.dart 中 的 _getLatestAvailableFlutterDate 方法中

有说明

// Cache is empty or it's been a while since the last server ping. Ping the server.
    try {
      final DateTime remoteFrameworkCommitDate = DateTime.parse(
        await FlutterVersion.fetchRemoteFrameworkCommitDate(channel),
      );
      await versionCheckStamp.store(
        newTimeVersionWasChecked: _clock.now(),
        newKnownRemoteVersion: remoteFrameworkCommitDate,
      );
      return remoteFrameworkCommitDate;
    } on VersionCheckError catch (error) {
      // This happens when any of the git commands fails, which can happen when
      // there's no Internet connectivity. Remote version check is best effort
      // only. We do not prevent the command from running when it fails.
      globals.printTrace('Failed to check Flutter version in the remote repository: $error');
      // Still update the timestamp to avoid us hitting the server on every single
      // command if for some reason we cannot connect (eg. we may be offline).
      await versionCheckStamp.store(
        newTimeVersionWasChecked: _clock.now(),
      );
      return null;
    }

将以上部分注释 ,直接 return null 就好.

修改了这部分代码,需要将 $FLUTTERHOME/bin/cache 下面 flutter_tools 相关的程序删除,重心执行 flutter 命令就会重新编译了.

后面手动更新 git 代码的时候需要 commit 掉本地修改,没有冲突的情况下都可以正常使用了.