You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级go_router至最新版本导致Flutter应用报错求助

问题

Flutter应用使用go_router实现导航,在7.1.1版本下运行正常,但升级至13.2.1版本后,nav.dart文件因queryParameters和location属性报错,这些属性在新版本中已被重构或移除。

涉及代码片段

_GoRouterStateExtensions扩展代码

extension _GoRouterStateExtensions on GoRouterState {
  Map<String, dynamic> get extraMap =>
      extra != null ? extra as Map<String, dynamic> : {};
  Map<String, dynamic> get allParams => <String, dynamic>{}
    ..addAll(pathParameters)
    ..addAll(queryParameters)
    ..addAll(extraMap);
  TransitionInfo get transitionInfo => extraMap.containsKey(kTransitionInfoKey)
      ? extraMap[kTransitionInfoKey] as TransitionInfo
      : TransitionInfo.appDefault();
}

RootPageContext类代码

class RootPageContext {
  const RootPageContext(this.isRootPage, [this.errorRoute]);
  final bool isRootPage;
  final String? errorRoute;

  static bool isInactiveRootPage(BuildContext context) {
    final rootPageContext = context.read<RootPageContext?>();
    final isRootPage = rootPageContext?.isRootPage ?? false;
    final location = GoRouter.of(context).location;
    return isRootPage &&
        location != '/' &&
        location != rootPageContext?.errorRoute;
  }

  static Widget wrap(Widget child, {String? errorRoute}) => Provider.value(
        value: RootPageContext(true, errorRoute),
        child: child,
      );
}

尝试过的无效操作

  • 将queryParameters替换为queryParams和queryParametersAll,仍报错
  • 尝试通过GoRouterState.of(context).location和GoRouterState.of(context).fullpath获取location属性,无效

报错信息

lib/controllers/flutter_flow/nav/nav.dart:163:14: Error: The getter 'queryParams' isn't defined for the class 'GoRouterState'.
 - 'GoRouterState' is from 'package:go_router/src/state.dart' ('/C:/.../lib/src/state.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'queryParams'.
    ..addAll(queryParams)
             ^^^^^^^^^^^
lib/controllers/flutter_flow/nav/nav.dart:309:48: Error: The getter 'location' isn't defined for the class 'GoRouterState'.
 - 'GoRouterState' is from 'package:go_router/src/state.dart' ('/C:/.../lib/src/state.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'location'.
    final location = GoRouterState.of(context).location;
                                               ^^^^^^^^
解决方案

go_router从7.x升级到13.x后,GoRouterState的API做了重大重构,针对你的问题,修改如下:

1. 修复查询参数获取

新版本中,GoRouterState的查询参数需要通过uri.queryParameters获取,替换原有的queryParameters:

修改后的_GoRouterStateExtensions:

extension _GoRouterStateExtensions on GoRouterState {
  Map<String, dynamic> get extraMap =>
      extra != null ? extra as Map<String, dynamic> : {};
  Map<String, dynamic> get allParams => <String, dynamic>{}
    ..addAll(pathParameters)
    ..addAll(uri.queryParameters) // 替换为uri.queryParameters
    ..addAll(extraMap);
  TransitionInfo get transitionInfo => extraMap.containsKey(kTransitionInfoKey)
      ? extraMap[kTransitionInfoKey] as TransitionInfo
      : TransitionInfo.appDefault();
}

如果需要获取包含重复键的查询参数(比如?id=1&id=2),可以改用uri.queryParametersAll,它返回Map<String, List<String>>,根据业务需求选择即可。

2. 修复路径location获取

新版本中GoRouterState不再直接提供location属性,需要通过uri.toString()获取完整路径,或通过GoRouter实例的路由信息提供者获取:

修改RootPageContext中的isInactiveRootPage方法:

static bool isInactiveRootPage(BuildContext context) {
  final rootPageContext = context.read<RootPageContext?>();
  final isRootPage = rootPageContext?.isRootPage ?? false;
  // 通过GoRouter实例获取当前完整路径
  final location = GoRouter.of(context).routeInformationProvider.value.uri.toString();
  return isRootPage &&
      location != '/' &&
      location != rootPageContext?.errorRoute;
}

如果能直接拿到GoRouterState实例,也可以用state.uri.toString()获取路径。

说明

go_router大版本升级时,将原有的location、queryParameters等属性整合到了uri对象中,更符合标准URI处理逻辑,修改后代码即可兼容13.2.1版本。

内容的提问来源于stack exchange,提问作者YUSOF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:56:06