Web 常见问题
在 Flutter 中编写或运行 Web 应用程序时遇到的一些问题,以及 Web 与不同之处。
常见问题
#在 Web 平台使用 Flutter 的场景有哪些?
#Flutter 目前并非适用于所有的网页内容,不过我们主要关注三个应用场景:
渐进式 web 应用 (Progressive web apps, PWA),兼具 web 的高覆盖面与桌面应用的强大功能。
单页应用 (Single page apps, SPA),只需一次加载,并与互联网服务动态互传数据。
将现有 Flutter 移动应用拓展到 web,在两个平台共享代码。
现在阶段,Flutter 不适合具有丰富文本和瀑布流的页面。例如,博客文章等基于流媒体的丰富文本内容,其受益于网络构建的以文档为中心的模式,而不是像 Flutter 这样的 UI 框架可以提供的以应用为中心的服务。然而,你可以使用 Flutter 将交互式体验嵌入到这些网站中。
有关如何在 Web 上使用 Flutter 的更多信息,参考文档: Flutter 的 Web 支持。
Flutter Web 应用的 SEO 优化
#一般情况下,Flutter Web 的目标是构建「动态化」网页应用。 Flutter 的 Web 端支持会优先考虑和确保性能、保真度和一致性。这意味着生成的网页页面可能不是搜索引擎「熟悉」的结构化页面。
不过,Jaspr(由社区发布的 Dart package)确实 支持静态网站。实际上,Dart 文档、Flutter 文档 以及 Flutter 官网首页 都已迁移使用 Jaspr package。
To summarize, for web content that is static or document-like, we recommend either using:
- Jaspr, if you want to use Dart but want a more traditional DOM-based website. Also note that Jaspr makes SEO work in the same way a traditional website would.
- HTML—in this case, consider separating your primary application experience (created in Flutter), from your landing page, marketing content, and help content (created using search engine optimized HTML).
Does hot reload work with a web app?
#Yes! For more information, check out hot reload on the web.
现在有哪些浏览器支持 Flutter 了?
#现在 Flutter web 应用可以运行在以下浏览器中:
Chrome(移动和桌面端)
Safari(移动和桌面端)
Edge(移动和桌面端)
Firefox(移动和桌面端)
在开发阶段,Chrome(在 macOS、Windows 以及 Linux)以及 Edge(在 Windows 上)将作为默认浏览器用于调试。
我可以在任意 IDE 中,构建、运行并发布 web 应用吗?
#你可以在 Android Studio/IntelliJ 和 VS Code 里选择使用 Chrome 或者 Edge 浏览器。
设备下拉列表里现在应该在所有平台里都包含了 Chrome (web)。
我该如何构建响应式 web 应用?
#请参阅 创建响应式应用。
我能在 Web 应用中使用 dart:io 这个 package 吗?
#
不行。文件系统在浏览器中是无法访问的。对于网络功能来说,请使用 http
package。请注意,安全方面的工作有所不同,因为浏览器(而不是应用程序)控制 HTTP 请求上的标头。
我应该如何处理一个 Web 平台特定的导入?
#部分插件需要在特定平台导入库或者文件,尤其是当使用浏览器无法访问的文件系统时。若要在你的应用里使用这些插件,请参阅 dart.cn: 选择性的导入。
Flutter Web 是否支持并发?
#Dart 通过 isolates 机制实现并发,目前在 Web 中尚未支持。
Flutter Web 没有内置并发的支持,但你可以尝试通过 web workers 来解决这个问题。
我该如何部署 Web 应用?
#请参阅 打包并发布到 Web 平台。
Platform.is API 现在可用吗?
#
No. While you can technically import dart:io when compiling for the web,
calling any Platform.isXYZ method throws an UnsupportedError.
Furthermore, importing dart:io in a package
(except through conditional imports) causes pub.dev
to score the package as not supporting the web.
-
If you are developing a Flutter app, consider using
kIsWeb. -
If you are developing a package
(especially one without a Flutter dependency),
consider using the
os_detectpackage.
Why doesn't my app update immediately after it's deployed?
#
You might need to configure the Cache-Control header
returned by your web server.
For example, if this header is set to 3600,
then the browser and CDN will cache the asset for 1 hour,
and your users might see an out-of-date
version of your app up to 1 hour after you deploy a new version.
For more information about caching on the web, check out
Prevent unnecessary network requests with the HTTP Cache.
It's a good idea to be aware of this behavior to avoid an undesirable user experience. After you deploy your app, users might use a cached version of your app (cached by the browser or CDN) for the duration defined by your cache headers. This can lead to using a version of your app that is incompatible with changes that have been deployed to backend services.
How do I clear the web cache after a deployment and force an app download?
#
If you wish to defeat these cache headers after each deployment,
a common technique is to append a build ID of some sort to the links
of your static resources, or update the filenames themselves.
For example, logo.png might become logo.v123.png.
<!-- Option 1, append build ID as a query parameter in your links -->
<script src="flutter_bootstrap.js?v=123" async></script>
<!-- Option 2, update the filename and update your links -->
<script src="flutter_bootstrap.v123.js" async></script>
Flutter doesn't currently support appending build IDs to resources automatically.
How do I configure my cache headers?
#If you are using Firebase Hosting, the shared cache (CDN) is invalidated when you deploy a new version of your app. However, to make sure that the browser doesn't cache application scripts but the shared cache does, you can configure your cache headers as follows,
{
"hosting": {
"headers": [
{
"source":
"**/*.@(jpg|jpeg|gif|png|svg|webp|css|eot|otf|ttf|ttc|woff|woff2|font.css)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=3600,s-maxage=604800"
}
]
},
{
"source":
"**/*.@(mjs|js|wasm|json)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=0,s-maxage=604800"
}
]
}
]
}
}
How do I configure a service worker?
#
The service worker generated by flutter build web is deprecated,
and you can disable it by setting the --pwa-strategy flag to none
when running the flutter build web command.
flutter build web --pwa-strategy=none
If you would like to continue to use a service worker, you can build your own or try third-party tools such as Workbox.
If your service worker is not refreshing,
configure your CDN and browser cache by setting
the Cache-Control header to a small value such as 0 or 60 seconds.
除非另有说明,本文档之所提及适用于 Flutter 3.41.2 版本。本页面最后更新时间:2026-03-19。查看文档源码 或者 为本页面内容提出建议。