Repository: flutter
Issue: [go_router] Duplicate GlobalKey detected in widget tree when a navigatorKey is used on a nested ShellRoute
Labels: package,P2,p: go_router,team-go_router,triaged-go_router

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Tested on
- go_router: 5.0.1

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Tap on BottomNavigationBar's buttons. (More faster than page transition) 


<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart

final GlobalKey<NavigatorState> _navigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'shell');

final router = GoRouter(
  initialLocation: '/a',
  routes: [
    ShellRoute(
      builder: (context, state, child) => HomePage(child: child),
      routes: [
        ShellRoute(
          navigatorKey: _navigatorKey,
          builder: (context, state, child) => child,
          routes: [
            GoRoute(
              path: '/a',
              builder: (context, state) => const PageA(),
            )
          ],
        ),
        GoRoute(
          path: '/b',
          builder: (context, state) => const PageB(),
        )
      ],
    )
  ],
);

void main() {
  runApp(
    MaterialApp.router(
      routeInformationParser: router.routeInformationParser,
      routeInformationProvider: router.routeInformationProvider,
      routerDelegate: router.routerDelegate,
    ),
  );
}

class HomePage extends StatelessWidget {
  const HomePage({super.key, required this.child});
  final Widget child;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: GoRouter.of(context).location.startsWith('/a') ? 0 : 1,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.navigate_before),
            label: 'A',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.navigate_next),
            label: 'B',
          )
        ],
        onTap: (value) {
          if (value == 0) {
            context.go('/a');
          } else {
            context.go('/b');
          }
        },
      ),
      body: child,
    );
  }
}

class PageA extends StatelessWidget {
  const PageA({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Colors.blue,
    );
  }
}

class PageB extends StatelessWidget {
  const PageB({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Colors.red,
    );
  }
}

```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue. If there is a crash, please include
      the entirety of the crash log including the backtrace.
-->

```


════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.

The key [LabeledGlobalKey<NavigatorState>#2b85e shell] was used by multiple widgets. The parents of those widgets were:
- Semantics(container: false, properties: SemanticsProperties, tooltip: null, renderObject: RenderSemanticsAnnotations#d87b0)
- Semantics(container: false, properties: SemanticsProperties, tooltip: null, renderObject: RenderSemanticsAnnotations#9f6a5)
A GlobalKey can only be specified on one widget at a time in the widget tree.
When the exception was thrown, this was the stack
#0      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:flutter/…/widgets/framework.dart:2883
#1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#2      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure>
package:flutter/…/widgets/framework.dart:2827
#3      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#4      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>
package:flutter/…/widgets/framework.dart:2822
#5      BuildOwner._debugVerifyGlobalKeyReservation
package:flutter/…/widgets/framework.dart:2891
#6      BuildOwner.finalizeTree.<anonymous closure>
package:flutter/…/widgets/framework.dart:2950
#7      BuildOwner.finalizeTree
package:flutter/…/widgets/framework.dart:3032
#8      WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:885
#9      RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:378
#10     SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1175
#11     SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1104
#12     SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:1015
#13     _invoke (dart:ui/hooks.dart:148:13)
#14     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:318:5)
#15     _drawFrame (dart:ui/hooks.dart:115:31)
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
Multiple widgets used the same GlobalKey.
════════════════════════════════════════════════════════════════════════════════

...

```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing app...                                                        
No issues found! (ran in 21.8s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 3.3.0, on macOS 12.5 21G72 darwin-x64, locale en-US)
    • Flutter version 3.3.0 on channel stable at /Users/tony/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (3 weeks ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/tony/Library/Android/sdk
    • Platform android-31, build-tools 30.0.1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13F100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • 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 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.71.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (3 available)
    • Tony Xr (mobile) • 00008020-000C11A23C02003A • ios            • iOS 15.4.1 19E258
    • macOS (desktop)  • macos                     • darwin-x64     • macOS 12.5 21G72 darwin-x64
    • Chrome (web)     • chrome                    • web-javascript • Google Chrome 97.0.4692.71

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->