i have a project about basic market app first screen adds item to market when clicked on add button adds to some collection and passes to market cart page in cart page lists the collection (item name,item price,quantity) and bottom of the screen shows total price.
im learning flutter and my coding is pretty bad right now
my problem is im dismissing items but items not removing from the screen. i tried unique key(does nothing and not working), i tried give keys manually this time ''A dismissed Dismissible widget is still part of the tree.''
i tried to remove items from lists that containts data but not working.
this is my seperated listview builder
Widget buildList() => ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.black,
),
itemCount: cartItemQuantity.length,
itemBuilder: (context, index) {
return seperated(
context,
cartItemNames[index],
recievedShopItemsList[index],
index,
cartItemQuantity[index],
recievedShopItemKeys[index],
);
},
);
this is my widget (when its dismissed its gonna remove item and update total price from screen(newPrice) )
Widget seperated(BuildContext context, String name, String price, int index, int quantity, String uKey) {
return Dismissible(
key: Key(uKey),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
widget.recievedCart.remove(name);
widget.recievedNewShopItems.remove(name);
setState(() {
newPrice= newPrice- (int.parse(price) * quantity);
});
},
child: ListTile(
title: Text(
name,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text("${price}.0 TL"),
trailing: Text("Quantiy : ${quantity}")),
);
im so swamped this is my full code of cart page
Map<String, dynamic> recievedCart = Map();
Map<String, dynamic> recievedShopItems = Map();
Map<String, dynamic> recievedNewShopItems = Map();
CartPage(
{required this.recievedCart,
required this.recievedShopItems,
required this.recievedNewShopItems});
@override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
var cartItemQuantity,
cartItemNames,
recievedShopItemsList,
recievedShopItemKeys;
num newPrice= 0;
num calculate() {
for (int i = 0; i < widget.recievedNewShopItems.length; i ) {
newPrice= newPrice
(int.parse(recievedShopItemsList[i]) * cartItemQuantity[i]);
}
return newPrice;
}
@override
void initState() {
// TODO: implement initState
cartItemQuantity = widget.recievedCart.values.toList();
cartItemNames = widget.recievedCart.keys.toList();
recievedShopItemsList = widget.recievedNewShopItems.values.toList();
recievedShopItemKeys = widget.recievedNewShopItems.keys.toList();
setState(() {
newPrice = calculate();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.pop(context);
},
),
title: Center(
child: Text("Cart"),
),
),
body: Column(
children: <Widget>[
Expanded(child: buildList()),
Container(
height: MediaQuery.of(context).size.height / 12,
color: Colors.blue,
child: Center(
child: Text(
"TOTAL : ${newPrice}.0 TL",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25),
),
),
)
],
),
);
}
int a = 0;
int b = 0;
int finalprice= 0;
Widget seperated(BuildContext context, String name, String price, int index,
int quantity, String uKey) {
int qnt = 0;
return Dismissible(
key: Key(uKey), //
direction: DismissDirection.endToStart,
onDismissed: (direction) {
print(uKey);
widget.recievedCart.remove(name);
widget.recievedNewShopItems.remove(name);
setState(() {
newPrice= newPrice- (int.parse(price) * quantity);
});
},
child: ListTile(
title: Text(
name,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text("${price}.0 TL"),
trailing: Text("Quantiy : ${quantity}")),
);
}
int index = 0;
int itemCount = 0;
Widget buildList() => ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.black,
),
itemCount: cartItemQuantity.length,
itemBuilder: (context, index) {
return seperated(
context,
cartItemNames[index],
recievedShopItemsList[index],
index,
cartItemQuantity[index],
recievedShopItemKeys[index],
);
},
);
}
CodePudding user response:
Change widget.recievedCart.remove(name) to widget.recievedCart.removeAt(index),
remove and removeAt are different:
remove(element)
removes a single element from the list, which is strictly equal to the element passed in.
removeAt(element)
removes the element from the list at the given index, and returns that element
