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}} 就会自动被替换为实际的版本号。

相关推荐
好的佩奇12 小时前
Dart 之增强枚举
android·flutter·dart
好的佩奇15 小时前
蓝牙之无线电与通信距离
android·flutter·蓝牙
程序员老刘·2 天前
从短剧App的bug看响应式编程在AI时代的优势
flutter·ai编程·跨平台开发
传奇开心果编程2 天前
【Flutter踩坑排雷】第7课:巨型 build 函数坑|全部 UI 写在同一个 build,维护难、隐性异常
android·flutter·ios·移动开发·跨平台·dart·声明式ui
程序员老刘2 天前
从短剧App的bug看响应式编程在AI时代的优势
flutter·ai编程·客户端
恋猫de小郭3 天前
最近 Android 有什么更新:Android Dev Summit 2026 技术解读
android·前端·flutter
好的佩奇3 天前
Dart 之类修饰符
android·flutter·dart
好的佩奇014 天前
Dart 之类修饰符
android·flutter·dart
Jewel1054 天前
文字为什么总是不居中!记录一个bug
android·flutter·ios
2601_949847755 天前
Form-data 跨端三方库仓颉适配全流程
前端·javascript·flutter