Flutter升级后index.html废弃警告的解决方法咨询
Flutter Web index.html 废弃警告修复方案
针对你遇到的两个废弃警告,以下是具体的修复步骤和修改后的代码示例:
1. 使用{{flutter_service_worker_version}}替换serviceWorkerVersion变量
- 删除原代码中定义
serviceWorkerVersion的整个script块:<script> // The value below is injected by flutter build, do not touch. const serviceWorkerVersion = null; </script> - 在引用该变量的位置,直接用模板令牌
{{flutter_service_worker_version}}替换原变量名。
2. 升级loadEntrypoint为load方法
FlutterLoader.load方法简化了初始化流程,无需嵌套回调,直接通过Promise链完成引擎初始化和应用启动:
- 将原
_flutter.loader.loadEntrypoint调用替换为_flutter.loader.load - 调整参数结构,移除
onEntrypointLoaded回调,改为直接链式调用initializeEngine()和runApp()
修改后的完整index.html代码
<!DOCTYPE html> <html> <head> <!-- If you are serving your web app in a path other than the root, change the href value below to reflect the base path you are serving from. The path provided below has to start and end with a slash "/" in order for it to work correctly. For more details: * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base This is a placeholder for base href that will be replaced by the value of the `--base-href` argument provided to `flutter build`. --> <base href="$FLUTTER_BASE_HREF"> <meta charset="UTF-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="description" content="chapter 04"> <!-- iOS meta tags & icons --> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-title" content="flutter_layout"> <link rel="apple-touch-icon" href="icons/Icon-192.png"> <!-- Favicon --> <link rel="icon" type="image/png" href="favicon.png"/> <title>flutter_layout</title> <link rel="manifest" href="manifest.json"> <!-- This script adds the flutter initialization JS code --> <script src="flutter.js" defer></script> </head> <body> <script> window.addEventListener('load', function(ev) { // Download main.dart.js _flutter.loader.load({ serviceWorker: { serviceWorkerVersion: "{{flutter_service_worker_version}}", } }).then(function(engineInitializer) { return engineInitializer.initializeEngine(); }).then(function(appRunner) { appRunner.runApp(); }); }); </script> </body> </html>
内容的提问来源于stack exchange,提问作者user3523406
相关产品推荐
相关产品推荐

