0

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.

5
  • You are not accessing a property id in your code snippet. Jan 5, 2020 at 0:07
  • What would be the best way to access it? Jan 5, 2020 at 0:21
  • I am afraid you did not understand: you stated that "id called on null" is popping up, but fact is that there is no .id call in your snippet - hope it is clear now :) You would need to include the section of your code, where the error occurs. Jan 5, 2020 at 0:29
  • Editing now. One moment please Jan 5, 2020 at 0:31
  • Updated code. Please review Jan 5, 2020 at 1:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.