I am trying to make a cart page using the provider package and i wanted to have a quantity of every single item so every time i add a new product i want it to check if it exists in the list and if it exists i want it to count how many times it is added but if its not already in the cart it gets added to the cart
this is my Cart class
import 'package:flutter/material.dart';
import 'item.dart';
class Cart extends ChangeNotifier{
List<Item> items = [];
double totalPrice = 0.0;
int singleProductCount = 0;
void add(Item item) {
items.add(item);
totalPrice += item.price;
notifyListeners();
}
void remove(Item item) {
items.remove(item);
totalPrice -= item.price;
notifyListeners();
}
int get count {
return items.length;
}
double get totalprice {
return totalPrice;
}
int get singleproductCount {
return singleProductCount;
}
List<Item> get basketItems {
return items;
}
}
and this is the IconButton that adds it to the cart
IconButton(icon: Icon(Icons.add), onPressed: () {
cart.add(Item(name: snapshot.data.documents[index]["name"], price: snapshot.data.documents[index]["price"], imageUrl: snapshot.data.documents[index]["imageUrl"]));
}),