Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate GlobalKey detected in widget tree

Tags:

flutter

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.

It seems like the issue is that Screen B is either

  • A) Not being disposed of correctly
  • B) Is not doing something that it otherwise could

And I don't actually know:

  • 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 documentation states: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.

Console Error Output:

══╡ 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. 

So this part of the error output:

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.

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);   } 
like image 221
Ashton Thomas Avatar asked Mar 19 '18 19:03

Ashton Thomas


People also ask

How do I fix duplicate GlobalKey detected in widget tree?

To Solve Duplicate GlobalKey detected in widget tree Error just Just remove static word where you define globe key. will solve your error.

How do I get rid of GlobalKey in flutter?

currentState!. dispose() actually disposes the State of the associated widget. You should not call this yourself. Instead, provide a new GlobalKey to the second subtree or remove the old one before navigating to the new page.

What is GlobalKey in flutter?

A key that is unique across the entire app. Global keys uniquely identify elements. Global keys provide access to other objects that are associated with those elements, such as BuildContext. For StatefulWidgets, global keys also provide access to State.


2 Answers

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

like image 84
flaugh Avatar answered Oct 12 '22 23:10

flaugh


Remove the static and final type from the key variable so if

static final GlobalKey<FormState> _abcKey = GlobalKey<FormState>(); 

change it to

GlobalKey<FormState> _abcKey = GlobalKey<FormState>(); 
like image 25
MindBrain Avatar answered Oct 12 '22 21:10

MindBrain