Check if List contains Item, if true increase quantity, if false add to list

Hey all, so i have the following setup:

Firestore Collection called menu:

  • item

  • price

  • description

    DataType called cartItemType:

  • menuReference

  • quantity

    App State called cart:

  • List of cartItemType

    Menu Page:

    All items of the menu collection get displayed in a Listview and every item has their own add to cart button.

    Now, i want to do the following. If the button is pressed, it should be checked if this specific item already exists in the cart. If it doesn't (button of this specific item has not been pressed before) that item has to be added to the list with quantity of 1.

    If it does exist in the list already (e.g. the same button has been clicked twice) the quantity inside of this specific product should be incremented by 1.

    If you press the same button 4 times, the quantity should be 4, but in the List of cartitems there should still be only one product.

    Now how would i approach this? I have tried to do a conditional statement, where it checks if the List of cartItemTypes already contains the menuRef, but it keeps telling me that this variable is not valid. However, i don't know why. Please help.

3
13 replies