I am stucking at this error nowhere i am used this key except all these places but still I got the error of
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<AnimatedListState>#520c0]
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:
- SizedBox(width: 500.0, height: 500.0, renderObject: RenderConstrainedBox#5a5f0 relayoutBoundary=up4)
A GlobalKey can only be specified on one widget at a time in the widget tree.
And also sometimes I got the error of
ultiple widgets used the same GlobalKey.
Here is the code where I use declare and used the animation key in the code.
final GlobalKey<AnimatedListState> _animationKey = GlobalKey();
` get animationKey => _animationKey;
void addItem(int index,CardModel cardModel){
_animationKey.currentState?.insertItem(index,duration: const Duration(milliseconds: 500));
AppGlobals.globalList.insert(index, cardModel);
}
void removeItem(int index, CardModel cardModel){
AppGlobals.globalList.removeAt(index);
if(index>0){
index == 1 ? _animationKey.currentState?.removeItem(index - 1 , (context, animation) {
// return SizeTransition(sizeFactor: animation,
return SizeTransition(
sizeFactor: animation,
child:CardContainer(currentCardModel: cardModel,index: index - 1 ,));
},
duration: const Duration(milliseconds: 300))
: _animationKey.currentState?.removeItem(index - 1, (context, animation) {
// return SizeTransition(sizeFactor: animation,
return SlideTransition(
position: animation.drive(
Tween<Offset>(begin: Offset(0, -index.toDouble()), end: const Offset(0,0.2))
.chain(CurveTween(curve: Curves.ease))),
child:CardContainer(currentCardModel: cardModel,index: index -1 ,));
},
duration: const Duration(milliseconds: 300));
}
else{
_animationKey.currentState?.removeItem(index , (context, animation) {
// return SizeTransition(sizeFactor: animation,
return SizeTransition(
sizeFactor: animation,
child:CardContainer(currentCardModel: cardModel,index: index ,));
},
duration: const Duration(milliseconds: 500));
}
}
return SizedBox(
height: 500,
width: 500,
child : AnimatedList(
key: homeScreenProvider.animationKey,
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
itemBuilder: ((context, index,animation) {
return SizedBox(
child: CardContainer(currentCardModel: AppGlobals.globalList[index],index: index,));
}),
initialItemCount: AppGlobals.globalList.length,
// initialItemCount: AppGlobals.globalList.length ,
),
);
Please anyone help me.
Trying to find out the solution of how to resolve the error of
════════ Exception caught by widgets library ═══════════════════════════════════
Duplicate GlobalKey detected in widget tree.
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
Multiple widgets used the same GlobalKey.
════════════════════════════════════════════════════════════════════════════════
in Flutter