Updating go_router package to the latest version causes errors in Flutter app

33 Views Asked by At

I have a Flutter app that uses the go_router package for navigation. The app was working fine with version 7.1.1 of go_router, but when I updated to the latest version (13.2.1), I started getting errors in my nav.dart file. The errors are related to the queryParameters and location properties, which seem to have been renamed or removed in the latest version.

_GoRouterStateExtensions code:

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 code:

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,
      );
}

I tried replacing queryParameters with queryParams and queryParametersAll, but both resulted in the same error. I also tried accessing the location property using GoRouterState.of(context).location and GoRouterState.of(context).fullpath, but neither worked.

I was expecting the code to compile successfully after updating the property names according to the latest version of go_router. However, I'm still getting errors, and I'm not sure how to resolve them. I would greatly appreciate any guidance on how to update my code to be compatible with the latest version of go_router and fix these errors.

The errors are as follows:

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;
                                               ^^^^^^^^
1

There are 1 best solutions below

0
Renato Urrea On

That's because at v10 the API changed, look at this.

go_router ChangeLog:

10.0.0 BREAKING CHANGE: Replaces location, queryParameters, and queryParametersAll in GoRouterState with Uri. See Migrating to 10.0.0 or run dart fix --apply to fix the breakages.