Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GlobalKey in CupertinoNavigationBar property triggers "Multiple widgets used the same GlobalKey." when changing route #113409

Open
Aulig opened this issue Oct 13, 2022 · 4 comments
Labels
f: cupertino flutter/packages/flutter/cupertino repository f: routes Navigator, Router, and related APIs. found in release: 3.3 Found to occur in 3.3 found in release: 3.5 Found to occur in 3.5 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-design Owned by Design Languages team triaged-design Triaged by Design Languages team

Comments

@Aulig
Copy link

Aulig commented Oct 13, 2022

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Click the settings icon button in the app bar

Expected results:
No error gets thrown, since doing the same with the equivalent material widgets (Scaffold, AppBar) throws no such error.

Actual results:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.
The key [GlobalKey#cf59a] was used by multiple widgets. The parents of those
widgets were different
widgets that both had the following description:
  IconTheme(color: systemBlue)
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/src/widgets/framework.dart:2883:13)
#1      _LinkedHashMapMixin.forEach
(dart:collection-patch/compact_hash.dart:617:13)
#2      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous
closure>.<anonymous closure>
(package:flutter/src/widgets/framework.dart:2827:20)
#3      _LinkedHashMapMixin.forEach
(dart:collection-patch/compact_hash.dart:617:13)
#4      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>
(package:flutter/src/widgets/framework.dart:2822:36)
#5      BuildOwner._debugVerifyGlobalKeyReservation
(package:flutter/src/widgets/framework.dart:2891:6)
#6      BuildOwner.finalizeTree.<anonymous closure>
(package:flutter/src/widgets/framework.dart:2950:11)
#7      BuildOwner.finalizeTree
(package:flutter/src/widgets/framework.dart:3032:8)
#8      WidgetsBinding.drawFrame
(package:flutter/src/widgets/binding.dart:885:19)
#9      RendererBinding._handlePersistentFrameCallback
(package:flutter/src/rendering/binding.dart:378:5)
#10     SchedulerBinding._invokeFrameCallback
(package:flutter/src/scheduler/binding.dart:1175:15)
#11     SchedulerBinding.handleDrawFrame
(package:flutter/src/scheduler/binding.dart:1104:9)
#12     SchedulerBinding._handleDrawFrame
(package:flutter/src/scheduler/binding.dart:1015:5)
#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)
════════════════════════════════════════════════════════════════════════════════
════════════════════

Another exception was thrown: Multiple widgets used the same GlobalKey.

Another exception was thrown: Multiple widgets used the same GlobalKey.

Another exception was thrown: Duplicate GlobalKey detected in widget tree.
Code sample
import 'package:flutter/cupertino.dart';

void main() => runApp(const CupertinoApp(home: Example()));

class Example extends StatelessWidget {
  const Example({Key? key}) : super(key: key);

  static final GlobalKey settingsButtonKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Container(),
      navigationBar: CupertinoNavigationBar(
          trailing: CupertinoButton(
            onPressed: () {
              Navigator.push(context, CupertinoPageRoute(builder: (context) => const SettingsPage()));
            },
            child: const Icon(CupertinoIcons.settings),
            key: settingsButtonKey,
          )),
    );
  }
}

class SettingsPage extends StatelessWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Container(),
      navigationBar: const CupertinoNavigationBar(middle: Text("Settings")),
    );
  }
}
Logs
[        ] R Hot restart.
[        ] h List all available interactive commands.
[        ] d Detach (terminate "flutter run" but leave application running).
[        ] c Clear the screen
[        ] q Quit (terminate the application on the device).
[        ]  Running with sound null safety 
[        ] An Observatory debugger and profiler on Pixel 3a is available at: http://127.0.0.1:60024/yUcbevZ8ywM=/
[ +557 ms] The Flutter DevTools debugger and profiler on Pixel 3a is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:60024/yUcbevZ8ywM=/
[+6800 ms]
                    ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
                    The following assertion was thrown while finalizing the widget tree:
                    Multiple widgets used the same GlobalKey.
                    The key [GlobalKey#cf82d] was used by multiple widgets. The parents of those widgets were different
                    widgets that both had the following description:
                      IconTheme(color: systemBlue)
                    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/src/widgets/framework.dart:2883:13)
                    #1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
                    #2      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/framework.dart:2827:20)
                    #3      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
                    #4      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure> (package:flutter/src/widgets/framework.dart:2822:36)
                    #5      BuildOwner._debugVerifyGlobalKeyReservation (package:flutter/src/widgets/framework.dart:2891:6)
                    #6      BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2950:11)
                    #7      BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:3032:8)
                    #8      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:885:19)
                    #9      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:378:5)
                    #10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
                    #11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
                    #12     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1015:5)
                    #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)
                    ════════════════════════════════════════════════════════════════════════════════════════════════════

No issues found! (ran in 7.5s)
[√] Flutter (Channel stable, 3.3.4, on Microsoft Windows [Version 10.0.19044.2130], locale de-DE)
    • Flutter version 3.3.4 on channel stable at D:\Programme\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eb6d86ee27 (9 days ago), 2022-10-04 22:31:45 -0700
    • Engine revision c08d7d5efc
    • Dart version 2.18.2
    • DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc4)
    • Android SDK at D:\Programme\AndroidSDK
    • Platform android-33, build-tools 31.0.0-rc4
    • ANDROID_HOME = D:\Programme\AndroidSDK
    • ANDROID_SDK_ROOT = D:\Programme\AndroidSDK
    • Java binary at: D:\Programme\AndroidStudio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.11.7)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.31911.196
    • Windows 10 SDK version 10.0.19041.0
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK

[√] Android Studio (version 2021.2)
    • Android Studio at D:\Programme\AndroidStudio
    • 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.12+7-b1504.28-7817840)

[√] IntelliJ IDEA Community Edition (version 2020.2)
    • IntelliJ at D:\Programme\IntelliJ IDEA Community Edition 2020.2
    • 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

[√] Connected device (4 available)
    • Pixel 3a (mobile) • 98XAY17YMW • android-arm64  • Android 12 (API 32)
    • Windows (desktop) • windows    • windows-x64    • Microsoft Windows [Version 10.0.19044.2130]
    • Chrome (web)      • chrome     • web-javascript • Google Chrome 106.0.5249.119
    • Edge (web)        • edge       • web-javascript • Microsoft Edge 106.0.1370.42

[√] HTTP Host Availability
    • All required HTTP hosts are available
@danagbemava-nc danagbemava-nc added the in triage Presently being triaged by the triage team label Oct 14, 2022
@danagbemava-nc danagbemava-nc changed the title GlobalKey in CupertinoNavigationBar triggers "Multiple widgets used the same GlobalKey." when changing route GlobalKey in CupertinoNavigationBar property triggers "Multiple widgets used the same GlobalKey." when changing route Oct 14, 2022
@danagbemava-nc
Copy link
Member

The issue is reproducible using the code sample provided above.

Adding the key to the CupertinoNavigationBar itself does not trigger the exception. It seems to be triggered when the key is added to a property of the CupertinoNavigationBar.
I tested with leading, trailing & middle, and the exception was thrown for all 3 properties.

Labeling for further investigation from the team.

logs
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown while finalizing the widget tree:
flutter: Multiple widgets used the same GlobalKey.
flutter: The key [GlobalKey#f6874 Example] was used by multiple widgets. The parents of those widgets were
flutter: different widgets that both had the following description:
flutter:   IconTheme(color: systemBlue)
flutter: A GlobalKey can only be specified on one widget at a time in the widget tree.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/framework.dart:2950:13)
flutter: #1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:620:13)
flutter: #2      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/framework.dart:2894:20)
flutter: #3      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:620:13)
flutter: #4      BuildOwner._debugVerifyGlobalKeyReservation.<anonymous closure> (package:flutter/src/widgets/framework.dart:2889:36)
flutter: #5      BuildOwner._debugVerifyGlobalKeyReservation (package:flutter/src/widgets/framework.dart:2958:6)
flutter: #6      BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:3017:11)
flutter: #7      BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:3099:8)
flutter: #8      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:870:19)
flutter doctor -v
[✓] Flutter (Channel stable, 3.3.4, on macOS 12.6 21G115 darwin-arm, locale en-GB)
    • Flutter version 3.3.4 on channel stable at /Users/nexus/dev/sdks/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eb6d86ee27 (9 days ago), 2022-10-04 22:31:45 -0700
    • Engine revision c08d7d5efc
    • Dart version 2.18.2
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

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

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

[✓] Android Studio (version 2021.3)
    • 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.13+0-b1751.21-8125866)

[!] Android Studio
    • Android Studio at /Applications/Android Studio Preview 2.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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/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.13+0-b1751.21-8125866)

[!] Android Studio
    • Android Studio at /Users/nexus/Downloads/Android Studio Preview.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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

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

[✓] Connected device (4 available)
    • Nexus (mobile)         • 00008020-001875E83A38002E            • ios            • iOS 16.0.2 20A380
    • iPhone 14 Pro (mobile) • 4302007A-B132-4B89-887A-C5993FCA3284 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 106.0.5249.119
    ! Error: Nexus is busy: Fetching debug symbols for Nexus. Xcode will continue when Nexus is finished. (code -10)

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
[✓] Flutter (Channel master, 3.5.0-6.0.pre.37, on macOS 12.6 21G115 darwin-arm64, locale en-GB)
    • Flutter version 3.5.0-6.0.pre.37 on channel master at /Users/nexus/dev/sdks/flutters
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 08e1729204 (12 hours ago), 2022-10-13 17:40:07 -0400
    • Engine revision 3711bbaeab
    • Dart version 2.19.0 (build 2.19.0-309.0.dev)
    • DevTools version 2.18.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

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

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

[✓] Android Studio (version 2021.3)
    • 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.13+0-b1751.21-8125866)

[!] Android Studio
    • Android Studio at /Applications/Android Studio Preview 2.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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/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.13+0-b1751.21-8125866)

[!] Android Studio
    • Android Studio at /Users/nexus/Downloads/Android Studio Preview.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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

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

[✓] Connected device (4 available)
    • Nexus (mobile)         • 00008020-001875E83A38002E            • ios            • iOS 16.0.2 20A380
    • iPhone 14 Pro (mobile) • 4302007A-B132-4B89-887A-C5993FCA3284 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm64
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 106.0.5249.119

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.

@danagbemava-nc danagbemava-nc added framework flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository f: routes Navigator, Router, and related APIs. has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 3.3 Found to occur in 3.3 found in release: 3.5 Found to occur in 3.5 and removed in triage Presently being triaged by the triage team labels Oct 14, 2022
@Piinks Piinks added the P1 High-priority issues at the top of the work list label Oct 20, 2022
@MitchellGoodwin
Copy link
Contributor

This is a known limitation of CupertinoNavigationBar because on the slide transition, a new nav bar is painted over the existing one, with the child widgets copied over. See the paragraph on transitionBetweenRoutes for more information, and work arounds.

However from this issue, and the duplicate one, it does seem like there's a need to be adjusted, but it will be a fairly sizable rewrite of the transition, so it may not be a quick fix.

@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-design Owned by Design Languages team triaged-design Triaged by Design Languages team and removed triaged-design Triaged by Design Languages team labels Jul 8, 2023
@flutter-triage-bot
Copy link

This issue is assigned but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

@flutter-triage-bot flutter-triage-bot bot added the Bot is counting down the days until it unassigns the issue label Jul 30, 2023
@MitchellGoodwin MitchellGoodwin added P2 Important issues not at the top of the work list and removed P1 High-priority issues at the top of the work list labels Aug 18, 2023
@MitchellGoodwin
Copy link
Contributor

I'm downgrading the priority to p2 as per my last comment. I'm also unassigning this from myself as I'm not actively working on this at the moment. If somebody wants to pick it up before I get to it, please feel free to tag me here or on Discord for help.

@MitchellGoodwin MitchellGoodwin removed their assignment Aug 18, 2023
@flutter-triage-bot flutter-triage-bot bot removed the Bot is counting down the days until it unassigns the issue label Aug 18, 2023
@MitchellGoodwin MitchellGoodwin added the triaged-design Triaged by Design Languages team label Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: cupertino flutter/packages/flutter/cupertino repository f: routes Navigator, Router, and related APIs. found in release: 3.3 Found to occur in 3.3 found in release: 3.5 Found to occur in 3.5 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-design Owned by Design Languages team triaged-design Triaged by Design Languages team
Projects
None yet
Development

No branches or pull requests

5 participants