gpt4 book ai didi

Flutter 从 GlobalKey 获取 Navigator 的当前路由名称

转载 作者:行者123 更新时间:2023-12-05 06:02:42 33 4
gpt4 key购买 nike

在我的 Flutter 应用程序中,我需要知道当前路径以避免两次推送相同的路径。

我发现我可以使用下面的代码:

    ModalRoute.of(context).settings.name

它有效,但仅适用于主导航器。

由于我的应用程序中有第二个导航器,我使用 GlobalKey 作为目标,我该如何访问它的当前路径?

我试过这样的:

ModalRoute.of(HomePage.navigatorKey.currentContext).settings.name

但它返回主要路线。

最佳答案

不知道你是否还需要它,但这是我解决问题的方法。

String route = "";
HomePage.navigatorKey.currentContext!.popUntil((currentRoute) {
route = currentRoute.settings.name;
// Return true so popUntil() pops nothing.
return true;
});

关于Flutter 从 GlobalKey 获取 Navigator 的当前路由名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66860721/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com