在导航中使用带有 GlobalKeys 的小部件

kik*_*kap 5 flutter

我的“主要”小部件必须具有全局键。如果我使用 apushNamed或等效项导航到它,它会生成关于小部件树中重复键的异常。我只能pop使用这个小部件,但这严重降低了我的导航选项和小部件的可重用性。

我已经包含了一个小型复制案例的来源,运行该应用程序,单击Login主页上的按钮,输入 3 个字符的登录名和 3 个字符的密码,然后Login.

有什么想法吗?在没有 的情况下重新设计GlobalKey是一项重大任务。

flutter: ??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ????????????????????????????????????????????????????????????
flutter: The following assertion was thrown while finalizing the widget tree:
flutter: Duplicate GlobalKey detected in widget tree.
flutter: The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of
flutter: the widget tree being truncated unexpectedly, because the second time a key is seen, the previous
flutter: instance is moved to the new location. The key was:
flutter: - [GlobalKey#1c91e navKey]
flutter: This was determined by noticing that after the widget with the above global key was moved out of its
flutter: previous parent, that previous parent never updated during this frame, meaning that it either did
flutter: not update at all or updated before the widget was moved, in either case implying that it still
flutter: thinks that it should have a child with that global key.
flutter: The specific parent that did not update after having one or more children forcibly removed due to
flutter: GlobalKey reparenting is:
flutter: - Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
flutter: hintOverrides: null, renderObject: RenderSemanticsAnnotations#147f5 NEEDS-PAINT)
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.finalizeTree.<anonymous closure> 
flutter: #1      BuildOwner.finalizeTree 
flutter: #2      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame 
flutter: #3      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback 
flutter: #4      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback 
flutter: #5      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame 
flutter: #6      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame 
flutter: #10     _invoke  (dart:ui/hooks.dart:236:10)
flutter: #11     _drawFrame  (dart:ui/hooks.dart:194:3)
flutter: (elided 3 frames from package dart:async)
flutter: ????????????????????????????????????????????????????????????????????????????????????????????????????
flutter: Another exception was thrown: Multiple widgets used the same GlobalKey.
Run Code Online (Sandbox Code Playgroud)
flutter: ??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ????????????????????????????????????????????????????????????
flutter: The following assertion was thrown while finalizing the widget tree:
flutter: Duplicate GlobalKey detected in widget tree.
flutter: The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of
flutter: the widget tree being truncated unexpectedly, because the second time a key is seen, the previous
flutter: instance is moved to the new location. The key was:
flutter: - [GlobalKey#1c91e navKey]
flutter: This was determined by noticing that after the widget with the above global key was moved out of its
flutter: previous parent, that previous parent never updated during this frame, meaning that it either did
flutter: not update at all or updated before the widget was moved, in either case implying that it still
flutter: thinks that it should have a child with that global key.
flutter: The specific parent that did not update after having one or more children forcibly removed due to
flutter: GlobalKey reparenting is:
flutter: - Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
flutter: hintOverrides: null, renderObject: RenderSemanticsAnnotations#147f5 NEEDS-PAINT)
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.finalizeTree.<anonymous closure> 
flutter: #1      BuildOwner.finalizeTree 
flutter: #2      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame 
flutter: #3      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback 
flutter: #4      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback 
flutter: #5      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame 
flutter: #6      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame 
flutter: #10     _invoke  (dart:ui/hooks.dart:236:10)
flutter: #11     _drawFrame  (dart:ui/hooks.dart:194:3)
flutter: (elided 3 frames from package dart:async)
flutter: ????????????????????????????????????????????????????????????????????????????????????????????????????
flutter: Another exception was thrown: Multiple widgets used the same GlobalKey.
Run Code Online (Sandbox Code Playgroud)

Mik*_*kin 0

当您使用pushNamed新路线时,新路线会添加到现有路线之上。

pushNamed您可以使用,而不是使用pushReplacementNamed,因此现有的小部件将从导航堆栈中删除并替换为新的。

  • 你尝试过吗?如果我修改示例,将对“pushNamed”的调用替换为对“pushReplacementNamed”的调用,我会得到完全相同的结果。 (2认同)
  • 这绝对不是“完美工作”。pushReplacementNamed 方法推送指定的路由,然后释放旧的。您仍然收到重复的 GlobalKey 错误。 (2认同)