【问题标题】:Flutter web - Window.history.pushState does not workFlutter web - Window.history.pushState 不起作用
【发布时间】:2021-02-13 14:01:23
【问题描述】:

我正在尝试让 Flutter Web 应用程序将查询参数附加到 url 的末尾(当在 UI 中进行选择时)而不重新加载页面

基本上我想在不重新加载页面的情况下将 URL 从 http://127.0.0.1:8080/#/sites 更新为 http://127.0.0.1:8080/#/sites?arg=1

以下行(import 'dart:html')在flutter中不起作用(URL根本没有改变)

 window.history.pushState(null, null, '?arg1=1');

我也试过 js 包 (https://pub.dev/packages/js) 也不行。

有没有办法让它在flutter web中工作,或者有没有其他方法可以满足我的要求?

【问题讨论】:

    标签: dart flutter-web


    【解决方案1】:

    经过一番挣扎,并尝试将调用包装在 Future.delayed 中似乎可以解决该问题。

    Future.delayed(
    Duration(milliseconds: 100), () => window.history.replaceState(null, null, '${Uri.base.toString()}?accountId=${selectedAccount.accountId}'));
    

    如果你有更好的答案,我很想知道。谢谢

    【讨论】:

    • 是的。在我看来,这就是现在正常工作的方法。 Duration.zero 也可以。
    猜你喜欢
    • 2022-01-24
    • 2020-04-08
    • 2021-08-27
    • 2020-08-18
    • 2021-10-06
    • 2012-11-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多