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

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

问题描述

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

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

  • 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对象被重用(而不是被 重新创建). 但是,为了有资格获得 嫁接时,必须将小部件插入到 从旧位置删除的动画帧.

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中失败:

  @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天全站免登陆