flutter web 如何确保用户收到更新

csharp 复制代码
flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[√] Flutter (Channel stable, 3.41.4, on Microsoft Windows [版本 10.0.26200.8039], locale zh-CN) [796ms]
    • Flutter version 3.41.4 on channel stable at D:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ff37bef603 (3 weeks ago), 2026-03-03 16:03:22 -0800
    • Engine revision e4b8dca3f1
    • Dart version 3.11.1
    • DevTools version 2.54.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android,
      enable-ios, cli-animations, enable-native-assets, omit-legacy-version-file, enable-lldb-debugging,
      enable-uiscene-migration

在老的版本中,flutter web 依靠文件哈希的方式控制版本更新,只要发现哈希不同,就会拉取最新的 js 文件。但最近这种方式已经被淘汰,main.dart.js 文件不再以有哈希后缀。

因此,用户端无法判断 main.dart.js 的版本。在开启缓存的情况下,用户可以无法更新到最新版本。直到缓存到期或者检测到 etag 不同。而 main.dart.js 非常大,如果频繁下载会非常影响用户体验。

想要控制 main.dart.js 版本的最简单方式就在拉取时补充版本信息,类似于 main.dart.js?version=1.2.3。那么要如何做到这一点呢?

如果你问 ai,他可能会告诉你,要修改 web/flutter_bootstrap.js 的 loadEntrypoint 方法的 entrypointUrl 参数:

vbnet 复制代码
entrypointUrl: '/main.dart.js?version=1.2.3',

但实际上这个参数已经过期了,顺带说一句,loadEntrypoint 方法也已经过期了。老的教程使用这个函数去自定义 flutter web 初始化。

javascript 复制代码
  /**
   * @deprecated
   * Loads flutter main entrypoint, specified by `entrypointUrl`, and calls a
   * user-specified `onEntrypointLoaded` callback with an EngineInitializer
   * object when it's done.
   *
   * @param {*} options
   * @returns {Promise | undefined} that will eventually resolve with an
   * EngineInitializer, or will be rejected with the error caused by the loader.
   * Returns undefined when an `onEntrypointLoaded` callback is supplied in `options`.
   */
  async loadEntrypoint(options) {
    const { entrypointUrl = resolveUrlWithSegments("main.dart.js"), onEntrypointLoaded, nonce } =
      options || {};
    return this._loadJSEntrypoint(entrypointUrl, onEntrypointLoaded, nonce);
  }

当前版本,flutter web 使用 _flutter.loader.load 方法去初始化。然而,新的方法并没有类似 entrypointUrl 用法的参数(注意不要和 entrypointBaseUrl 搞混)。

但可以看到,flutter 实际拉取 main.dart.js 的逻辑是这样的:

ini 复制代码
      const mainPath = build.mainJsPath ?? "main.dart.js";
      const entrypointUrl = resolveUrlWithSegments(entrypointBaseUrl, mainPath);
      return this._loadJSEntrypoint(entrypointUrl, onEntrypointLoaded, nonce);

我们只需要在 web/flutter_bootstrap 中自己去更改 build.mainJsPath 的值就好:

ini 复制代码
_flutter.buildConfig.builds[0].mainJsPath = 'main.dart.js?version=1.2.3';

那么如何自动更改这个版本号呢?其实 flutter web 自带了一些模板代码的设置,首先将上面的代码改为:

ini 复制代码
_flutter.buildConfig.builds[0].mainJsPath = 'main.dart.js?version={{version}}';

将打包代码改为:

ini 复制代码
flutter build web --web-define=version=1.2.3

{{version}} 就会自动被替换为实际的版本号。

相关推荐
恋猫de小郭2 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
好的佩奇3 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
SoaringHeart4 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
恋猫de小郭4 天前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
程序员老刘5 天前
跨平台开发地图 | 2026年11月
flutter·ai编程·客户端
传奇开心果编程5 天前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
恋猫de小郭5 天前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
好的佩奇6 天前
Dart 之可迭代对象与生成器
android·flutter·dart
恋猫de小郭6 天前
Flutter Analyzer 系列坑推进,必看,AI 时代这玩意最吃你内存
android·前端·flutter
LinXunFeng7 天前
拿竞品 App 一扫,技术栈全暴露了
android·flutter·开源