gpt4 book ai didi

flutter - 在小部件树中检测到重复的 GlobalKey

转载 作者:IT老高 更新时间:2023-10-28 12:42:28 25 4
gpt4 key购买 nike

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

似乎问题在于 Screen B 要么是

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

其实我也不知道:

  • 如果我只是删除 globalKey 的使用,会发生什么坏事? (以便更好地了解基础知识)
  • 如何正确解决此问题?

StatefulWidget 文档指出:enter link description here

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

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

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

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

最佳答案

就我而言,它喜欢热重载错误。只需重新启动调试对我有用。

关于flutter - 在小部件树中检测到重复的 GlobalKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com