Im working on an app where users can add items to a cart and i need to be able to clear the cart if the user so wishes.
The representation uses a GridLayout where I represent each item as a stateful GridItemCard (so as to maintain a count of the items added to the cart, which is a Singleton class with a List that contains the items added.
I am able to add and remove items, and maintain state in general but when I try to clear the cart, I need to update each GridItemCard to reset it to 0. (I maintain a manual counter inside for each item's count).
I now need to access the individual instances of the GridItemCards, and research indicates that accessing the Keys of each is better than adding them to a list in the main class and trying to access them.
Research:
I used the following SO answer to build the logic :
- Controlling State from outside of a StatefulWidget
- call method in one stateful widget from another stateful widget - Flutter
Based on this, list clearing should work using Keys to access but for some reason accessing the key from a list doesn't work. What is it that I'm missing?
class GridItemCard extends StatefulWidget {
final Item item;
final Function addItem;
final Function removeItem;
final Function clearItem;
//final int? itemCount;
final int? regular;
final int? regularStarch;
final int? stainRemoval;
final int? stainRemovalStarch;
GridItemCard({ required this.item, required this.addItem, required this.removeItem, required this.clearItem, Key? key, this.regular, this.regularStarch, this.stainRemoval, this.stainRemovalStarch}) : super(key: key);
@override
GridItemCardState createState() => GridItemCardState();
}
class GridItemCardState extends State<GridItemCard> with AutomaticKeepAliveClientMixin {
int count = 0;
int regular = 0;
int regularStarch = 0;
int stainRemoval = 0;
int stainRemovalStarch = 0;
//int regularLive = widget.items[]
@override
void initState() {
super.initState();
regular = widget.regular ?? 0;
regularStarch = widget.regularStarch ?? 0;
stainRemoval = widget.stainRemoval ?? 0;
stainRemovalStarch = widget.stainRemovalStarch ?? 0;
count = regular + regularStarch + stainRemoval + stainRemovalStarch;
}
void clearCount(){
regular =0;
regularStarch = 0;
stainRemoval =0;
stainRemovalStarch = 0;
count = 0;
}
To add the items and Keys, I do the following :
Widget getGridCard(Color color, Map<String, dynamic> item, String category) {
Item thisItem = Item.fromJson(item);
String itemId = thisItem.id ?? "";
print(category);
var ret = cart.itemCounter(itemId, category);
int regular = ret.$1;
int regularStarch = ret.$2;
int stainRemoval = ret.$3;
int stainRemovalStarch = ret.$4;
int count = regular + regularStarch + stainRemoval + stainRemovalStarch;
setState(() {});
final gcKey = GlobalKey<GridItemCardState>();
GridItemCard gc = GridItemCard(
item: thisItem,
addItem: addItem,
removeItem: removeItem,
key: gcKey,
regular: regular,
regularStarch: regularStarch,
stainRemoval: stainRemoval,
stainRemovalStarch: stainRemovalStarch,
);
allYourGCsAreBelongToUs.add(gcKey);
return gc;
}
However when I try using the Key to access the element and the embedded method, I get a blank:
void clearCart(){
cart.clear(); // clear the data in the Cart SingleTon
// now loop through the GC list and clear each cart's display
for(GlobalKey gc in allYourGCsAreBelongToUs){
gc.currentState.clearCount();
}
setState(() {
});
}
The error I get is :
lib/shopping_cart.dart:870:23: Error: The method 'clearCount' isn't defined for the class 'State?'.
- 'State' is from 'package:flutter/src/widgets/framework.dart' ('/opt/flutter/packages/flutter/lib/src/widgets/framework.dart').
- 'StatefulWidget' is from 'package:flutter/src/widgets/framework.dart' ('/opt/flutter/packages/flutter/lib/src/widgets/framework.dart'). Try correcting the name to the name of an existing method, or defining a method named 'clearCount'. gc.currentState.clearCount();