globalKey在我导航Screen A到Screen B单击"取消"按钮返回后,我遇到了错误Screen A.
这似乎是问题是,Screen B要么是
我实际上并不知道:
globalKey?(为了更好地理解基本面)StatefulWidget文档说明:在此输入链接描述
如果StatefulWidget的创建者使用GlobalKey作为其键,则StatefulWidget在从树中的一个位置移动到另一个位置时保持相同的State对象.由于具有GlobalKey的窗口小部件可以在树中的至多一个位置使用,因此使用GlobalKey的窗口小部件最多只有一个关联元素.当通过将与该窗口小部件关联的(唯一)子树从旧位置移植到新位置(而不是在该位置重新创建子树)时,框架利用此属性将全局键从树中的一个位置移动到另一个位置时利用此属性.新的位置).与StatefulWidget关联的State对象与子树的其余部分一起被移植,这意味着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 …Run Code Online (Sandbox Code Playgroud) flutter ×1