如何在Flutter中获取当前路由名称?

4

我是一名新手 Flutter 开发者,我该如何获取当前路由名称?我在两个不同的路由中有一个共同的列表视图。我想根据路由隐藏列表中的某些属性。

请帮助我。


这可能会对你有所帮助:https://dev59.com/6VYO5IYBdhLWcg3wBtEp - haresh
2个回答

12

试试这个:

var route = ModalRoute.of(context);

if(route!=null){
  print(route.settings.name);
}

为什么它返回 null 呢? - Ahmed Elsayed
请在错误的位置编写代码。将其作为脚手架的子级。 - Mehrdad
2
我发现它返回null,因为我没有在路由中使用名称。 - Ahmed Elsayed

0
路径:
Router.of(context).routeInformationProvider?.value.location

名称:

ModalRoute.of(navKey.currentContext).settings.name.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接