Skip to content

[go_router_builder] Pass GoRouteData.navigatorKey to generated GoRoute parentNavigatorKey #124497

@yevyevyev

Description

@yevyevyev

Hello, currently when I override navigatorKey for GoRouteData it is not passed to GoRoute parentNavigatorKey, thus resulting in inability to push child routes on "top" of shelf route.

Dependency versions
go_router: ^6.5.5
go_router_builder: ^1.2.0
build_runner: ^2.3.3
flutter doctor -v
[✓] Flutter (Channel stable, 3.7.5, on macOS 12.6 21G115 darwin-arm64, locale en-GB)
    • Flutter version 3.7.5 on channel stable at /Users/yev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c07f788888 (7 weeks ago), 2023-02-22 17:52:33 -0600
    • Engine revision 0f359063c4
    • Dart version 2.19.2
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/yev/Library/Android/sdk
    • Platform android-31, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] VS Code (version 1.77.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 12.6 21G115 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 112.0.5615.49

[✓] HTTP Host Availability
    • All required HTTP hosts are available
Minimal reproducible code
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

part 'main.g.dart';

final shellNavigatorKey = GlobalKey<NavigatorState>();
final rootNavigatorKey = GlobalKey<NavigatorState>();

@TypedShellRoute<AppHomeRoute>(routes: [
  TypedGoRoute<ListRoute>(path: ListRoute.path, routes: [
    TypedGoRoute<ListDetailsRoute>(path: ListDetailsRoute.path),
  ]),
])
class AppHomeRoute extends ShellRouteData {
  @override
  GlobalKey<NavigatorState>? get navigatorKey => shellNavigatorKey;

  @override
  Widget builder(BuildContext context, GoRouterState state, Widget navigator) =>
      Scaffold(
        appBar: AppBar(
          title: const Text('Shell route'),
        ),
        body: navigator,
      );
}

class ListRoute extends GoRouteData {
  static const path = '/list';

  @override
  Widget build(BuildContext context, GoRouterState state) => ListView(
      children: List.generate(5, (index) => index)
          .map(
            (e) => ListTile(
              onTap: () => ListDetailsRoute().go(context),
              title: Text('Item $e'),
            ),
          )
          .toList());
}

class ListDetailsRoute extends GoRouteData {
  static const path = 'details';

  @override
  // expected result is that this scaffold would be pushed on top of shell route scaffold
  // the bug is that it is pushed using the shell route key, not the rootNavigatorKey
  Widget build(BuildContext context, GoRouterState state) => Scaffold(
        appBar: AppBar(title: const Text('Details')),
        body: const Center(
          child: Text('Details'),
        ),
      );

  // explicitly specifying here that the navigator key should be root
  @override
  GlobalKey<NavigatorState>? get navigatorKey => rootNavigatorKey;
}

void main() {
  final router = GoRouter(
    initialLocation: ListRoute.path,
    navigatorKey: rootNavigatorKey,
    routes: $appRoutes,
  );
  runApp(MaterialApp.router(routerConfig: router));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.7Found to occur in 3.7found in release: 3.9Found to occur in 3.9has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_router_builderThe go_router_builder packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions