Flutter Web - 无法通过浏览器的后退和前进按钮导航
Posted
技术标签:
【中文标题】Flutter Web - 无法通过浏览器的后退和前进按钮导航【英文标题】:Flutter Web - Can't navigate through browser back and forward buttons 【发布时间】:2021-08-15 11:56:24 【问题描述】:在 Flutter Web 中,我无法浏览浏览器的后退和前进按钮。它给出了以下错误:
Error: Assertion failed: org-dartlang-sdk:///flutter_web_sdk/lib/_engine/engine/navigation/history.dart:284:14
_userProvidedRouteName != null
is not true
at Object.throw_ [as throw] (http://localhost:54382/dart_sdk.js:5344:11)
at Object.assertFailed (http://localhost:54382/dart_sdk.js:5280:15)
at _engine.SingleEntryBrowserHistory.new.onPopState (http://localhost:54382/dart_sdk.js:149735:59)
Error: Assertion failed: org-dartlang-sdk:///flutter_web_sdk/lib/_engine/engine/navigation/history.dart:284:14
_userProvidedRouteName != null
is not true
at Object.throw_ [as throw] (http://localhost:54382/dart_sdk.js:5344:11)
at Object.assertFailed (http://localhost:54382/dart_sdk.js:5280:15)
at _engine.SingleEntryBrowserHistory.new.onPopState (http://localhost:54382/dart_sdk.js:149735:59)
【问题讨论】:
【参考方案1】:如this answer中所述:
onWillPop 导航到新页面
class webScope extends StatelessWidget
@override
Widget build(BuildContext context)
return new WillPopScope(
onWillPop: () async => Navigator.push(context, MaterialPageRoute(builder: (context) => new NewPageWidget())),
child: Scaffold(
appBar: new AppBar(
title: new Text("webScope"),
),
),
);
或查看问题中的其他答案以完全阻止导航。
【讨论】:
如果用户可以从不同的页面导航到同一页面,那么当用户按下后退按钮时,他/她应该被导航到他或她来自的页面。像 navigator.pop 一样……这是最有效的方法吗? 也许这篇文章可以帮助你:medium.com/codechai/…以上是关于Flutter Web - 无法通过浏览器的后退和前进按钮导航的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:来自后退导航的回调产生“错误状态:调用关闭后无法添加新事件”
java web系统安全退出后点击浏览器后退按钮还会跳转到刚才浏览页面
浏览器的后退按钮不触发 parseRouteInformation 并抛出 Duplicate GlobalKey detected in widget tree.error Flutter