在我从 Screen A
导航到 Screen B
并单击“取消”按钮返回到 Screen A
之后,我遇到了 globalKey
错误。
看起来问题在于 Screen B
要么是
我也不知道:
globalKey
的使用,会发生什么坏事情?(以便更好地理解基本原理)StatateWidget 文档状态: 在这里输入链接描述
当从一个状态对象移动时,状态小部件保持相同的状态对象 如果其创建者使用 GlobalKey 用于 因为一个具有 GlobalKey 的小部件最多只能在一个 位置,使用 GlobalKey 的小部件最多只有一个 GlobalKey 框架利用了这个属性 当从树中的一个位置移动具有全局键的小部件时 通过嫁接与之关联的(唯一的)子树来实现 Widget 从旧位置到新位置(而不是 在新位置重新创建子树) 关联的语句与其他语句一起嫁接 子树,这意味着 State 对象被重用(而不是 然而,为了符合下列条件: 中的新位置插入小部件 同样的动画框 ,它从旧的位置被移除
控制台错误输出:
══╡ 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.
这部分的错误输出:
前一个父级在此帧期间从未更新,这意味着它 要么根本没有更新,要么在移动小部件之前就更新了
让我觉得我的旧 Stateful 小部件有机会做一些事情(或者重新定位自己或者释放一些东西以便正确处理。
这似乎在 assert(_children.contains(child))
台的 framework.dart
节目中失败了:
@override
void forgetChild(Element child) {
assert(_children.contains(child));
assert(!_forgottenChildren.contains(child));
_forgottenChildren.add(child);
}