Add removeFromCart function
This commit is contained in:
parent
2926e1ee38
commit
de612bc1c6
1 changed files with 21 additions and 0 deletions
|
@ -17,4 +17,25 @@ Future<void> addToCart(productId) async {
|
|||
cart.add(filteredCart[0]);
|
||||
}
|
||||
await prefs.setString('cart', jsonEncode(cart).toString());
|
||||
}
|
||||
|
||||
Future<void> removeFromCart(productId, bool batch) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
var cart = jsonDecode(prefs.getString('cart') ?? '[]');
|
||||
var filteredCart = cart.where((x) => x["product_id"] == productId).toList();
|
||||
if (filteredCart.length > 0) {
|
||||
if (batch) {
|
||||
cart = cart.where((x) => x["product_id"] != productId).toList();
|
||||
} else {
|
||||
filteredCart[0]["quantity"]--;
|
||||
if (filteredCart[0]["quantity"] == 0) {
|
||||
cart = cart.where((x) => x["product_id"] != productId).toList();
|
||||
} else {
|
||||
cart = cart.where((x) => x["product_id"] != productId).toList();
|
||||
cart.add(filteredCart[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
await prefs.setString('cart', jsonEncode(cart).toString());
|
||||
}
|
Loading…
Reference in a new issue