Future<Map<String, int>> addProductItemToShoppingCart( ProductItem productItemToAdd, int quantity, ) async { final prefs = await SharedPreferences.getInstance(); Map shoppingCartMap = getShoppingCartMap(prefs);
// Checking if it contian the productItem has already in the shopping cart.. bool alreadyInShoppingCart = shoppingCartMap.containsKey(productItemToAdd.id);
Future<Map<String, int>> updateProductItemQuantityInShoppingCart( String productItemIdToRemove, int quantity, ) async { final prefs = await SharedPreferences.getInstance(); Map shoppingCartMap = getShoppingCartMap(prefs);
// Checking if it contian the productItem already. bool inShoppingCart = shoppingCartMap.containsKey(productItemIdToRemove); if (!inShoppingCart) // "Product item is not in the shopping cart. Add product item to shopping cart first" throw Exception( "Item is not in the shopping cart." );
if (quantity > 0) { shoppingCartMap[productItemIdToRemove] = quantity; } else { // Removing the product item from shopping cart if given value is negative.; shoppingCartMap.remove(productItemIdToRemove); }
// Store back to pref; bool saveSuccessfully = await setShoppingCartMap(prefs, shoppingCartMap); return saveSuccessfully ? shoppingCartMap : null; }