notifyListeners resets my list view builder on function call

45 Views Asked by At

The function getAllData gets call from the start so I have data to work with. Next I click on a plus icon using the add function , everytime I use the add function with the notifier the data I call disapears . when I dont use the notifier the data gets update and is still there. Go to anothere screen and back ,data is updatad .

Tried alternatives or to replace notifyListeners. Nothing works

 final List _shopItems = [];
  List _specialItems = [];
  List _allItems = [];
  String userType = "PUBLIC";
  var productRef = FirebaseFirestore.instance.collection('productList');
//Get all productdata
  void getProductData() {
    if (_shopItems.length < 1) {
      productRef.get().then((QuerySnapshot snapshot) {
        snapshot.docs.forEach((DocumentSnapshot doc) {
          //    _shopItems.add(doc.data());
          _allItems.add(doc.data());
        });
      });
    } else {
      // dont do anything
    }
  }
 // list of cart items
  List _cartItems = [];

  get cartItems => _cartItems;

  get allItems => _allItems;

  get shopItems => _shopItems;

  get specialItems => _specialItems;

  // add item to cart
  void addItemToCart(int index, String product) {
    var checkItemExists = _cartItems.any((item) => item["product"] == product);
    //print(checkItemExists);
    if (!checkItemExists) {
      // Normal Product
      cartItems == cartItems.add(allItems[index]);

      incDecProductAmount(product, "+");

    } else {
      incDecProductAmount(product, "+");
    } /* */

    notifyListeners();
  }

//Increase or decrease the product
  void incDecProductAmount(String product, String type) {
    int productIndex =
        cartItems.indexWhere((item) => item["product"] == product);

    var totalProductAmount = int.parse(cartItems[productIndex]["amount"]);

    if (type == "+") {
      totalProductAmount++;
    } else {
      totalProductAmount--;
      if (totalProductAmount == 0) {
        cartItems.removeAt(productIndex);
        return;
      }

      if (totalProductAmount < 1) {
        totalProductAmount = 0;
      }
    }

    cartItems[productIndex]["amount"] = totalProductAmount.toString();
  }
0

There are 0 best solutions below