[go_router] Duplicate GlobalKey detected in widget tree when a navigatorKey is used on a nested ShellRoute

This issue has been created since 2022-09-22.

Tested on

  • go_router: 5.0.1

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Tap on BottomNavigationBar's buttons. (More faster than page transition)
Code sample
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,
    );
  }
}
Logs


════════ 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.
════════════════════════════════════════════════════════════════════════════════

...

Analyzing app...                                                        
No issues found! (ran in 21.8s)
[✓] 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!
More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-10-05
Star Count 145512
Watcher Count 3569
Fork Count 23398
Issue Count 11205

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
UI does not update if a template is deleted or added via CLI 4 2022-06-20 2022-10-04
Overwrite image data in page? 2 2021-01-14 2022-05-10
Preserving singlet dimensions when loading ImageJ hyperstack tifs 8 2020-08-03 2022-05-10
Exception when writing contiguous OME-TIFF 7 2020-11-04 2022-08-07
How to read TIFF files from MicroManager with memory-mapping 9 2021-01-11 2022-08-31
How to write GPSTags via tifffile.TiffWriter.write()? 3 2020-10-08 2022-05-10
Files produced in ScanImage 2020.1 cannot be parsed. 11 2020-12-11 2022-05-10
Replace `quarto tools info <toolname>` 0 2022-07-14 2022-10-05
Can I use R variables in quarto YAML? 30 2022-07-14 2022-10-04
List of 3D Points from the Dense Point Cloud 4 2022-02-21 2022-07-28
Update compiled dlib library for Fedora 2 2021-05-18 2022-08-29
Semeval15、Semeval16数据集 1 2021-04-18 2022-09-21
PHP Fatal error: Uncaught MySQLReplication\Socket\SocketException 0 2021-11-10 2022-07-15
Add support for decentralized Exchanges 1 2021-08-16 2022-08-05
Temps pour une nouvelle version? 3 2022-03-01 2022-07-19
ChineseSubFinder调用错误 physical video file not found 1 2022-06-14 2022-09-25
Sorting programmatically does not apply 2 2021-11-10 2022-07-22
Add component editor 0 2021-11-02 2022-10-02
Add theme editor 0 2021-11-02 2022-01-02
Add urls editor 0 2021-11-02 2022-01-02
Keepalived VIP conflict 12 2021-09-17 2022-09-17
atr_ missing from member initialization list 1 2022-05-27 2022-10-04
unable to replace the string about:reader?url= 12 2021-01-29 2022-09-11
Polygon gas estimations is wrong for executing transactions 1 2022-03-31 2022-07-03
Could not load recent projects 1 2022-01-25 2022-08-13
Could not initialize project 1 2021-10-21 2022-09-03
Package name 6 2021-10-04 2022-01-05
NoSuchMethodError for asserting two objects since 3.20.0 version 4 2022-06-03 2022-09-15
Build fails with missing "alexandria" dependency 13 2021-10-12 2022-08-23
Importing types causes error "TS2693" in `.vue` files 1 2021-08-10 2022-09-09
Drop support for Python 3.6 by requiring a more recent IPython version 0 2021-12-07 2022-09-15
docs(wip): new docs site available at https://vite-plugin-pwa.netlify.app/ 24 2021-07-03 2022-10-03
default export not working on pure-ESM typescript (node v16) 1 2022-09-10 2022-09-30
[ru] agreement false positive 0 2022-03-30 2022-08-28
Further information about validation 2 2022-03-21 2022-10-05
Kotlinx and custom serializer 10 2021-07-28 2022-09-14
[2.4] Race condition in (many) users joining from waiting room 5 2022-03-11 2022-09-04
Support one to many relation 2 2022-01-14 2022-09-14
Impossible to assume a role without ACCESSKEY And SecretKey 0 2021-07-27 2022-10-02
[A11y] ClearIndicator violation 2 2021-09-23 2022-10-05
Default binary file folder 1 2022-01-06 2022-09-17
RestClient Date deserialization option 2 2020-08-27 2022-09-17
[FR] add display sleep 11 2022-03-13 2022-10-02
Add functionality to resize allocations 2 2020-08-07 2022-10-03
Dependency Dashboard 0 2021-02-13 2022-10-05
Falled to Install Onesignal Cordova Plugin in IOS for create ipa file 3 2021-10-13 2022-09-23
Node to Auth connection count increase 0 2022-10-04 2022-09-28
Source code could not be parsed (Unexpected UTF-8 character) 5 2022-01-22 2022-07-17
How to determine protocol used for request? 2 2022-07-31 2022-09-30
Add Dashboard view to complete the UX design for tracing a transfer end-to-end 0 2021-10-11 2022-07-31