ios - Flutter webview navigationDelegate 调用了两次
问题描述
我正在尝试使用 Flutter Web 视图下载包含特定 url 模式的文件。这可行,但在这种情况下,浏览器打开了两次,因为 navigationDelegate 被调用了两次。isForMainFrame
NavigationRequest 对象除了属性是相同的。第一次是假的,第二次是真的。
CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(middle: Text(_appTitle)),
child: Container(
child: SafeArea(
child: IndexedStack(
index: _stackToView,
children: <Widget>[
WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: this._connectionString,
onPageStarted: (value) => setState(() {
if (shouldChangeStack) {
_stackToView = 1;
} else {
_stackToView = 0;
}
}),
onPageFinished: (value) => setState(() {
_stackToView = 0;
}),
navigationDelegate: (NavigationRequest request) async {
print(request.url);
if (request.url.contains("download")) {
setState(() {
shouldChangeStack = false;
});
if (await canLaunch(request.url)) {
await launch(request.url);
}
return NavigationDecision.prevent;
} else {
setState(() {
shouldChangeStack = true;
});
return NavigationDecision.navigate;
}
},
),
Container(
child: Center(
child: CircularProgressIndicator(),
),
)
],
),
top: true,
),
),
);
解决方案
委托方法被调用两次的原因是因为setState()
被调用。这会导致整体Widget build()
被重建。正如前面在评论中提到的,解决此问题的方法是在启动页面之前设置检查器并定义是否需要打开页面。
推荐阅读
- python - 使用 pyinstaller 转换为 .exe 后退出 pygame 窗口且未打开控制台时“无法执行脚本 myscript”
- java - 可以向 void 方法添加返回参数会破坏现有功能吗?
- javascript - 如何上传带有某些字段的图像?
- asp.net - 在 asp.net 应用程序的 web.config 文件中设置“mailto”地址?
- javascript - .join 不是函数
- javascript - 重新加载点击功能在javascript中不起作用
- javascript - 从 javascript 中的 javascript 方法解析 xml 输出
- javascript - 用jquery隐藏div
- mysql - Mysql更新行删除int二级列间隙
- c# - 用于 python 的 C# GUI 挂起