在小部件树中检测到重复的 GlobalKey [英] Duplicate GlobalKey detected in widget tree

查看:118
本文介绍了在小部件树中检测到重复的 GlobalKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Screen A 导航到 Screen B 并单击取消"按钮返回后,我遇到了 globalKey 错误到屏幕A.

I am running into a globalKey error after I navigate from Screen A to Screen B and click a "Cancel" button to go back to Screen A.

问题似乎在于 Screen B 要么是

It seems like the issue is that Screen B is either

  • A) 未正确处理
  • B) 没有做本来可以做的事情

其实我也不知道:

  • 如果我不使用 globalKey 会发生什么坏事?(以便更好地了解基本原理)
  • 我该如何正确解决这个问题?
  • What bad things are happening if I just remove the use of a globalKey? (as to get a better understanding of the fundamentals)
  • How can I correctly resolve this issue?

StatefulWidget 文档说明:在此处输入链接描述

StatefulWidget documentation states:enter link description here

StatefulWidget 从一个对象移动时保持相同的 State 对象树中的位置到另一个,如果它的创建者使用 GlobalKey它的关键.因为一个带有 GlobalKey 的小部件最多可以用于一个在树中的位置,使用 GlobalKey 的小部件最多有一个关联元素.框架利用了这个特性从树中的一个位置移动带有全局键的小部件时通过嫁接与之相关的(唯一)子树到另一个小部件从旧位置到新位置(而不是在新位置重新创建子树).状态对象与 StatefulWidget 关联的其他部分一起嫁接子树,这意味着 State 对象被重用(而不是被重新创建)在新位置.但是,为了有资格获得嫁接,widget必须插入到新位置从旧位置移除的相同动画帧.

A StatefulWidget keeps the same State object when moving from one location in the tree to another if its creator used a GlobalKey for its key. Because a widget with a GlobalKey can be used in at most one location in the tree, a widget that uses a GlobalKey has at most one associated element. The framework takes advantage of this property when moving a widget with a global key from one location in the tree to another by grafting the (unique) subtree associated with that widget from the old location to the new location (instead of recreating the subtree at the new location). The State objects associated with StatefulWidget are grafted along with the rest of the subtree, which means the State object is reused (instead of being recreated) in the new location. However, in order to be eligible for grafting, the widget must be inserted into the new location in the same animation frame in which it was removed from the old location.

控制台错误输出:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Duplicate GlobalKey detected in widget tree.
The following GlobalKey was 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 key was:
- [LabeledGlobalKey<FormFieldState<String>>#3c76d]
This was determined by noticing that after the widget with the above global key was moved out of its
previous parent, that previous parent never updated during this frame, meaning that it either did
not update at all or updated before the widget was moved, in either case implying that it still
thinks that it should have a child with that global key.
The specific parent that did not update after having one or more children forcibly removed due to
GlobalKey reparenting is:
- Column(direction: vertical, mainAxisAlignment: start, crossAxisAlignment: center, renderObject:
RenderFlex#7595c relayoutBoundary=up1 NEEDS-PAINT)
A GlobalKey can only be specified on one widget at a time in the widget tree.

所以这部分错误输出:

在此帧期间之前的父级从未更新,这意味着它要么根本没有更新,要么在移动小部件之前更新

previous parent never updated during this frame, meaning that it either did not update at all or updated before the widget was moved

让我觉得我的旧有状态小部件有机会做一些事情(重新定位自己或释放一些东西以正确处理.

makes me think there was some opportunity for my old Stateful widget to do something (either reposition itself or release something as to be disposed correctly.

这似乎在 assert(_children.contains(child)) 上的 framework.dart 中失败:

This seems to be failing in framework.dart on assert(_children.contains(child)):

  @override
  void forgetChild(Element child) {
    assert(_children.contains(child));
    assert(!_forgottenChildren.contains(child));
    _forgottenChildren.add(child);
  }

推荐答案

就我而言,它就像一个热重载错误.只需重新启动调试即可.

In my case, it likes a hot reload bug. Just restart debugging works for me.

这篇关于在小部件树中检测到重复的 GlobalKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆