I created an inherited widget that acts at cart for my restaurant app. I am trying to check if the locations are the same by checking if there is a current location. If not overwrite the entire cart with new data or just add another product. But, every time I try to code a check, I get "id called on null". I want to make sure that only products from the location are called so here is my code:
Cart itemCart;
Cart get userCart => itemCart;
CartDetails orderDetail;
CartProvider cart;
Cart currentOrder;
bool pendingOrder = false;
enum CartErrors { productAlreadyExists, successfullyAdded }
class Cart {
int restaurantId;
String restaurantImage;
String productImage;
String productName;
Restaurant restaurant;
List<CartDetails> orderDetails;
Cart(
{this.restaurantId,
this.restaurantImage,
this.restaurant,
this.productImage,
this.productName,
List<CartDetails> products})
: orderDetails = products;
void addToCart(
restaurant, resId, restaurantImage, CartDetails product, total) {
if (resId == itemCart.restaurantId) {
orderDetails.add(product);
} else {
orderDetails = <CartDetails>[];
orderDetails.add(product);
itemCart = Cart(
restaurant: restaurant,
restaurantId: resId,
restaurant: restaurantImage,
products: orderDetails,
);
_addProduct(CartErrors.successfullyAdded);
}
}
void _addProduct(error) {
switch (error) {
case CartErrors.successfullyAdded:
print('Product Added');
break;
case CartErrors.productAlreadyExists:
print('Product Already Added');
break;
}
}
I'm checking to see if itemCart currently has an order from a different location. If so, overwrite, if not add another.
id
in your code snippet..id
call in your snippet - hope it is clear now :) You would need to include the section of your code, where the error occurs.