Flutter中如何使用go_router获取当前路由的名称?

问题描述 投票:0回答:1

按照迁移指南中描述的更改,我可以直接访问 Uri(例如 /product/123)。但是,我需要获取这条路线的name,即product_details。

flutter flutter-go-router
1个回答
0
投票

我使用了未解决问题中提到的自定义扩展,该扩展仍然处于开放状态,因为 .location() 不起作用或在迁移后被删除/弃用 https://github.com/flutter/flutter/issues/129833:

extension GoRouterExtension on GoRouter {
  String location() {
    final RouteMatch lastMatch = routerDelegate.currentConfiguration.last;
    final RouteMatchList matchList = lastMatch is ImperativeRouteMatch
        ? lastMatch.matches
        : routerDelegate.currentConfiguration;
    final String location = matchList.uri.toString();
    return location;
  }
}

然后只需使用如下所示的上下文即可访问该位置。

   final GoRouter route = GoRouter.of(context);
   final String location = route.location();
© www.soinside.com 2019 - 2024. All rights reserved.