Open
Description
I'm getting exceptions when using widgets that contain GlobalKeys inside of some widgets, in particular DropdownButton. Am I using it wrong? Based on what I have found on the web, this appears to be the way its supposed to be used.
Here's a simple repro:
code sample
import 'package:flutter/material.dart';
void main() => runApp(GlobalKeyTestApp());
class GlobalKeyTestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
'/': (context) => TestWidget(),
},
);
}
}
class TestWidget extends StatefulWidget {
@override
TestWidgetState createState() => TestWidgetState();
}
class TestWidgetState extends State<TestWidget> {
List<DropdownMenuItem> items = List<DropdownMenuItem>();
@override
Widget build(BuildContext contets) {
if (items.length == 0) {
print("BUILD ITEMS (this only happens once)");
int i = 0;
while (i < 3) {
items.add(
DropdownMenuItem(
child: KeyedSubtree(
key: GlobalKey(debugLabel: "item" + i.toString()),
child:
Container(
child: Text("item " + i.toString())
),
),
value: i
)
);
i = i + 1;
}
}
return Scaffold(body:
DropdownButton(
hint: Text("hint"),
items: items,
onChanged: (value) {
print("SET TO " + value.toString());
}
)
);
}
}
Steps to Reproduce
Run the app and click the dropdown. You'll see an exception thrown internally.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Duplicate GlobalKeys detected in widget tree.
The following GlobalKeys were specified multiple times in the widget tree. This will lead to parts
of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous
instance is moved to the new location. The keys were:
- [GlobalKey#ceb94 item0]
[GlobalKey#599b0 item1]
[GlobalKey#d040d item2]
Expected results:
It not throwing exceptions.
Actual results:
It throws exceptions.
Here's the result of flutter run --verbose
[✓] Flutter (Channel stable, 2.0.4, on Microsoft Windows [Version 10.0.19042.870], locale en-US)
• Flutter version 2.0.4 at C:\Users\Taha\Code\flutter_stable
• Framework revision b1395592de (11 days ago), 2021-04-01 14:25:01 -0700
• Engine revision 2dce47073a
• Dart version 2.12.2
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\Taha\Code\SDK
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = C:\Users\Taha\Code\SDK
• Java binary at: C:\Users\Taha\Code\android-studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[✓] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.2)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.9.31112.23
• Windows 10 SDK version 10.0.19041.0
[✓] Android Studio (version 4.1.0)
• Android Studio at C:\Users\Taha\Code\android-studio
• 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-b01)
[✓] IntelliJ IDEA Community Edition (version 2021.1)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.3
• Flutter plugin version 55.0.5
• Dart plugin version 211.6693.108
[✓] VS Code (version 1.55.1)
• VS Code at C:\Users\Taha\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.21.0
[✓] Connected device (4 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.870]
• Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.114
• Edge (web) • edge • web-javascript • Microsoft Edge 89.0.774.68
! Doctor found issues in 1 category.
(.venv) braxton@pop-os:~/code/flutter_app/bug$ flutter run --verbose
[ +39 ms] executing: uname -m
[ +19 ms] Exit code 0 from: uname -m
[ ] x86_64
[ +7 ms] executing: [/home/braxton/snap/flutter/common/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +3 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[ ] 8264cb3e8a797eef39cbcd32bb56fd07790efb7f
[ ] executing: [/home/braxton/snap/flutter/common/flutter/] git tag --points-at 8264cb3e8a797eef39cbcd32bb56fd07790efb7f
[ +6 ms] Exit code 0 from: git tag --points-at 8264cb3e8a797eef39cbcd32bb56fd07790efb7f
[ ] 2.1.0-12.1.pre
[ +23 ms] executing: [/home/braxton/snap/flutter/common/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[ +3 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[ ] origin/dev
[ ] executing: [/home/braxton/snap/flutter/common/flutter/] git ls-remote --get-url origin
[ +2 ms] Exit code 0 from: git ls-remote --get-url origin
[ ] https://github.com/flutter/flutter.git
[ +18 ms] Unable to locate an Android SDK.
[ +2 ms] executing: [/home/braxton/snap/flutter/common/flutter/] git rev-parse --abbrev-ref HEAD
[ +2 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[ ] dev
[ +31 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ +1 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +40 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +36 ms] Multiple devices found:
[ +32 ms] Linux (desktop) • linux • linux-x64 • Linux
[ ] Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.114
[ ] [1]: Linux (linux)
[ ] [2]: Chrome (chrome)
[ ] Please choose one (To quit, press "q/Q")
[ ] :
[+5534 ms] 2
[ +6 ms] Skipping pub get: version match.
[ +67 ms] Generating /home/braxton/code/flutter_app/bug/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[ +56 ms] Launching lib/main.dart on Chrome in debug mode...
[ +65 ms] Updating assets
[ +39 ms] Waiting for connection from debug service on Chrome...
[ +71 ms] <- reset
[ +3 ms] /home/braxton/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart --disable-dart-dev /home/braxton/snap/flutter/common/flutter/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot --sdk-root /home/braxton/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/ --incremental
--target=dartdevc --debugger-module-names --experimental-emit-debug-metadata -DFLUTTER_WEB_AUTO_DETECT=true --output-dill /tmp/flutter_tools.CUKVSZ/flutter_tool.QDRJAN/app.dill --libraries-spec file:///home/braxton/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/libraries.json --packages
/home/braxton/code/flutter_app/bug/.dart_tool/package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts --track-widget-creation --filesystem-root /tmp/flutter_tools.CUKVSZ/flutter_tools.ZBAADP --filesystem-scheme org-dartlang-app --initialize-from-dill
build/c7922d95bf4a2462b75c84a97c312edb.cache.dill.track.dill --platform file:///home/braxton/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk.dill --no-sound-null-safety
[ +7 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+10607 ms] Waiting for connection from debug service on Chrome... (completed in 10.7s)
[ ] Synced 29.3MB.
[ ] <- accept
[ ] Caching compiled dill
[ +84 ms] Using Google Chrome 89.0.4389.114
[ +121 ms] [CHROME]:
[ ] [CHROME]:DevTools listening on ws://127.0.0.1:36197/devtools/browser/7f5d7e07-73f0-4be0-9a13-798b92a36127
[ +170 ms] DwdsInjector: Received request for entrypoint at http://localhost:44287/main_module.bootstrap.js
[ +3 ms] MetadataProvider: Loading debug metadata...
[ +7 ms] MetadataProvider: Loaded debug metadata (no sound null safety)
[ +4 ms] DwdsInjector: Injected debugging metadata for entrypoint at http://localhost:44287/main_module.bootstrap.js
[ +861 ms] ChromeProxyService: Initializing expression compiler for main_module.bootstrap.js with sound null safety: false
[ +284 ms] DevHandler: Debug service listening on ws://127.0.0.1:46709/mqXld0dydsQ=/ws
[ +4 ms] Debug service listening on ws://127.0.0.1:46709/mqXld0dydsQ=/ws
[ ] Running with unsound null safety
[ ] For more information see https://dart.dev/null-safety/unsound-null-safety
[ +1 ms] 🔥 To hot restart changes while running, press "r" or "R".
[ ] For a more detailed help message, press "h". To quit, press "q".
[+1119 ms] BUILD ITEMS (this only happens once)
[+2095 ms] ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[ ] The following assertion was thrown while finalizing the widget tree:
[ ] Duplicate GlobalKeys detected in widget tree.
[ +1 ms] The following GlobalKeys were specified multiple times in the widget tree. This will lead to parts
[ ] of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous
[ ] instance is moved to the new location. The keys were:
[ ] - [GlobalKey#aaccd item0]
[ ] [GlobalKey#d1585 item1]
[ ] [GlobalKey#33e95 item2]
[ ] This was determined by noticing that after widgets with the above global keys were moved out of
[ ] their respective previous parents, those previous parents never updated during this frame, meaning
[ ] that they either did not update at all or updated before the widgets were moved, in either case
[ ] implying that they still think that they should have a child with those global keys.
[ ] The specific parents that did not update after having one or more children forcibly removed due to
[ +1 ms] GlobalKey reparenting are:
[ ] - Align(alignment: AlignmentDirectional.centerStart, dependencies: [Directionality], renderObject:
[ ] RenderPositionedBox#42fa4 relayoutBoundary=up11 NEEDS-PAINT)
[ ] Align(alignment: AlignmentDirectional.centerStart, dependencies: [Directionality], renderObject:
[ ] RenderPositionedBox#18a54 relayoutBoundary=up11 NEEDS-PAINT)
[ ] Align(alignment: AlignmentDirectional.centerStart, dependencies: [Directionality], renderObject:
[ ] RenderPositionedBox#3c2fe relayoutBoundary=up11 NEEDS-PAINT)
[ ] 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:
[ ] dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49 throw_
[ ] packages/flutter/src/widgets/framework.dart 2896:15 <fn>
[ ] packages/flutter/src/widgets/framework.dart 2920:16 finalizeTree
[ ] packages/flutter/src/widgets/binding.dart 877:7 drawFrame
[ ] packages/flutter/src/rendering/binding.dart 329:5 [_handlePersistentFrameCallback]
[ ] packages/flutter/src/scheduler/binding.dart 1144:15 [_invokeFrameCallback]
[ ] packages/flutter/src/scheduler/binding.dart 1082:9 handleDrawFrame
[ ] packages/flutter/src/scheduler/binding.dart 998:5 [_handleDrawFrame]
[ ] lib/_engine/engine/platform_dispatcher.dart 931:13 invoke
[ ] lib/_engine/engine/platform_dispatcher.dart 147:5 invokeOnDrawFrame
[ ] lib/_engine/engine.dart 262:45 <fn>
[ ] ════════════════════════════════════════════════════════════════════════════════════════════════════
[+1063 ms] SET TO 1
flutter doctor
[✓] Flutter (Channel dev, 2.1.0-12.1.pre, on Linux, locale en_US.UTF-8)
• Flutter version 2.1.0-12.1.pre at /home/braxton/snap/flutter/common/flutter
• Framework revision 8264cb3e8a (4 weeks ago), 2021-03-10 12:37:57 -0800
• Engine revision 711ab3fda0
• Dart version 2.13.0 (build 2.13.0-116.0.dev)
[✗] Android toolchain - develop for Android devices
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[✓] Chrome - develop for the web
• Chrome at google-chrome
[✓] Linux toolchain - develop for Linux desktop
• clang version 10.0.0-4ubuntu1
• cmake version 3.16.3
• ninja version 1.10.0
• pkg-config version 0.29.1
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
[✓] Connected device (2 available)
• Linux (desktop) • linux • linux-x64 • Linux
• Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.114
! Doctor found issues in 2 categories.
Metadata
Metadata
Assignees
Labels
Issues that are less important to the Flutter projectflutter/packages/flutter/material repository.Found to occur in 2.0Found to occur in 2.1flutter/packages/flutter repository. See also f: labels.The issue has been confirmed reproducible and is ready to work onOwned by Design Languages teamTriaged by Design Languages team