惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Martin Fowler
Martin Fowler
爱范儿
爱范儿
IT之家
IT之家
Last Week in AI
Last Week in AI
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
The Cloudflare Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog

博客园 - 孤独的猫咪神

自定义贪吃蛇环境进行PPO模型训练 通过TensorBoard进行PPO参数优化 TensorBoard PPO log 解析 macos(M4)上跑强化学习 - PyTorch LunarLander-v3 Stable-Baselines3 HarmonyOS中,html 与 ets 桥接沟通 Flutter中,html 与 dart 桥接沟通 Flutter 环境变量 Mole,清理Mac的小工具 Flutter自定义主题Theme最佳实践 Ubuntu 20.04 开机自动添加git的ssh 米家 + arduino + 自定义服务器 Flutter 第三方库 Jenkins CLI 通过ssh方式链接时的证书 阿里云Ubuntu 14.04 + Nginx + .net core + MySql 移动开发网络接口 经验总结 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问 Pathoto项目:AWS+golang+beego搭建 osx开发,skport项目记录 使用Jekyll在Github上搭建博客 iOS搜索附近的位置(类似微博朋友圈位置) retrofit2中ssl的Trust anchor for certification path not found问题 Android中的Semaphore React Native 在现有项目中的探路 博客园 Linux客户端 2.0 正式发布! 博客园 Windows客户端 2.0 正式发布! 博客园 Mac客户端 2.0 正式发布!
部分机型下,Flutter的flutter_inappwebview在使用evaluateJa...
孤独的猫咪神 · 2026-07-02 · via 博客园 - 孤独的猫咪神

在上次的调用中, Flutter中,html 与 dart 桥接沟通
flutter_inappwebview的Dart调用html方法,使用的是postMessage方式进行的,如下代码:

// 使用 postMessage 发送
webViewController.evaluateJavascript('''
window.postMessage($message, '*');
''');

但在最近的开发中,发现在部分机型下(目前遇到了:真我手机是这样),这个调用是无效的。

解决方案

通过源码发现:
evaluateJavascript方法中,有2个参数:

  1. 我们当前使用的source,也就是要执行的js代码
  2. ContentWorld,这个是可选参数,也就是这段js的作用域。

其中的ContentWorld对象,在注释中表示,Android的实现是通过<iframe>来实现的。也标注了,若需要访问windowdocument,需要使用window.topwindow.top.document来进行。

我使用的是6.1.5的版本,源码 content_world.dart 注释如下:

// ...  这里省略了

// 以下的内容从第3行开始
///Class that represents an object that defines a scope of execution for JavaScript code and which you use to prevent conflicts between different scripts.
///
///**NOTE for iOS**: available on iOS 14.0+. This class represents the native [WKContentWorld](https://developer.apple.com/documentation/webkit/wkcontentworld) class.
///
///**NOTE for Android**: it will create and append an `<iframe>` HTML element with `id` attribute equals to `flutter_inappwebview_[name]`
///to the webpage's content that contains only the inline `<script>` HTML elements in order to define a new scope of execution for JavaScript code.
///Unfortunately, there isn't any other way to do it.
///There are some limitations:
///- for any [ContentWorld], except [ContentWorld.PAGE] (that is the webpage itself), if you need to access to the `window` or `document` global Object,
///you need to use `window.top` and `window.top.document` because the code runs inside an `<iframe>`;
///- also, the execution of the inline `<script>` could be blocked by the `Content-Security-Policy` header.
class ContentWorld {
// ...  这里省略了
}

所以,根据注释的标注,将发送的方法改为:

// 使用 postMessage 发送
webViewController.evaluateJavascript('''
window.top.postMessage($message, '*');
''');

经过测试,可以成功的发送postMessage了。
另:其他使用window.postMessage可以使用的情况,使用window.top.postMessage也同样可以使用,不用做机型判断。iOS也可以的。