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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 孤独的猫咪神

HarmonyOS中,html 与 ets 桥接沟通 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中,html 与 dart 桥接沟通
孤独的猫咪神 · 2026-06-03 · via 博客园 - 孤独的猫咪神

当前有两种webview的插件,分别是:

  • webview_flutter
  • flutter_inappwebview

webview_flutter

dart 调用 html 方法

dart

其实就是dart向html发送消息,使用webViewController.runJavaScript方法,执行一段js脚本。

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

若为map,需要先转换为json字符串,避免引号冲突。

html

const handleMessage = (event: MessageEvent) => {
const data = event.data
// 解析消息内容...
}

// 添加窗口消息监听器
window.addEventListener('message', handleMessage)

html 调用 dart 方法

dart

通过JavaScriptChannel,来实现监听html发送的消息的功能。
先向页面添加 JavaScriptChannel

//添加 JavaScript 通道用于接收 H5 消息
webViewController.addJavaScriptChannel('FlutterChannel',
onMessageReceived: (JavaScriptMessage message) {
handleH5Message(message.message); // 这就是处理html发送的消息的方法
});

html

这里的 FlutterChannel 是在dart端通过addJavaScriptChannel方法添加的,在网页端不需要声明。

// 使用 FlutterChannel 通道发送消息(匹配 Flutter 端的 JavaScriptChannel)
try {
if ((window as any).FlutterChannel) {
(window as any).FlutterChannel.postMessage(JSON.stringify(message))
console.log('通过 FlutterChannel 发送消息到 Flutter:', message)
} else {
console.warn('FlutterChannel 未定义,无法发送消息到 Flutter')
}
} catch (error) {
console.error('发送到 Flutter 失败:', error)
}

flutter_inappwebview

dart 调用 html 方法

dart

其实就是dart向html发送消息,使用webViewController.evaluateJavascript方法,执行一段js脚本。

// 使用 postMessage 发送
webViewController?.evaluateJavascript(
source: "window.postMessage($value, '*');",
);

若为map,需要先转换为json字符串,避免引号冲突。

html

const handleMessage = (event: MessageEvent) => {
const data = event.data
// 解析消息内容...
}

// 添加窗口消息监听器
window.addEventListener('message', handleMessage)

html 调用 dart 方法

dart

通过JavaScriptChannel,来实现监听html发送的消息的功能。
先向页面添加 JavaScriptChannel

// 添加 JavaScript Handler 用于接收 H5 消息
webViewController!.addJavaScriptHandler(
handlerName: 'FlutterChannel', // <--- 这个需要和 html 中的 handlerName 一致
callback: (message) {
if (message.isEmpty) {
return;
}
handleH5Message(message[0]);
},
);

html

这里的 FlutterChannel 是在dart端通过addJavaScriptChannel方法添加的,在网页端不需要声明。

// 使用 flutter_inappwebview 通道发送消息(匹配 Flutter 端的 JavaScriptChannel)
try {
if ((window as any).flutter_inappwebview) {
// 调用 Dart 端的 handlerName 并发送数据
(window as any).flutter_inappwebview.callHandler('FlutterChannel', message)
console.log('use flutter_inappwebview send to Flutter:', message)
} else {
console.warn('flutter_inappwebview undefine')
}
} catch (error) {
console.error('Send to Flutter failed: ', error)
}