将 go_router 包更新到最新版本会导致 Flutter 应用程序出现错误

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

我有一个 Flutter 应用程序,它使用 go_router 包进行导航。该应用程序在 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,
      );
}

我尝试用 queryParams 和 queryParametersAll 替换 queryParameters,但两者都导致了相同的错误。我还尝试使用 GoRouterState.of(context).location 和 GoRouterState.of(context).fullpath 访问 location 属性,但都不起作用。

我希望根据最新版本的 go_router 更新属性名称后代码能够成功编译。但是,我仍然收到错误,并且不知道如何解决它们。我非常感谢有关如何更新我的代码以与最新版本的 go_router 兼容并修复这些错误的指导。

错误如下:

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;
                                               ^^^^^^^^
flutter dart flutter-dependencies flutter-navigation flutter-go-router
1个回答
0
投票

那是因为在 v10 中 API 发生了变化,看看这个。

go_router 变更日志:

10.0.0 重大变化: 将 GoRouterState 中的 location、queryParameters 和 queryParametersAll 替换为 Uri。 请参阅迁移到 10.0.0 或运行 dart fix --apply 来修复损坏。

© www.soinside.com 2019 - 2024. All rights reserved.