Q:

Concurrent modification during iteration: Instance(length:5) of '_GrowableList'. Flutter

I'm trying to do a change to a list in flutter, but it keeps giving me this error :
Concurrent modification during iteration: Instance(length:5) of '_GrowableList'.

I've searched and didn't find an answer or solution that fits the situation
Here I smy code:
 void addProductsToCart() {
    int addedAmount = cardNum * widget.currentProduct.price;
    String note = noteTextController.text;
    int total = cardNum * widget.currentProduct.price;
    OrderItem orderItemInstance = OrderItem(
        productName: widget.currentProduct.name,
        productPrice: widget.currentProduct.price.toString(),
        productQuantity: cardNum.toString(),
        productNotes: note,
        productSupplierName: widget.supplierName,
        productImage: widget.currentProduct.image,
        totalPrice: total.toString());
    int theTotal = widget.cart.total;
    if (widget.cart.orderDetails.length > 0){
      for (var item in widget.cart.orderDetails){
        if (item.productName == orderItemInstance.productName){
          item.productQuantity += orderItemInstance.productQuantity;
          item.totalPrice += orderItemInstance.totalPrice;
          theTotal += addedAmount;
        } else {
          widget.cart.orderDetails.add(orderItemInstance);
          theTotal += addedAmount;
        }
      }
    } else {
      theTotal += addedAmount;
      widget.cart.orderDetails.add(orderItemInstance);
    }
    widget.cart.total += total;
    setState(() {});
  }

Based on the debugger in android studio . the error is being catched in one of the lines that adds or removes data from the list
Like this :
      theTotal += addedAmount;
      widget.cart.orderDetails.add(orderItemInstance);

A:

I've solved it by delaying the add part after finishing the loop .. here is the working code :
void addProductsToCart() {
    int addedAmount = cardNum * widget.currentProduct.price;
    String note = noteTextController.text;
    int total = cardNum * widget.currentProduct.price;
    OrderItem orderItemInstance = OrderItem(
        productName: widget.currentProduct.name,
        productPrice: widget.currentProduct.price.toString(),
        productQuantity: cardNum.toString(),
        productNotes: note,
        productSupplierName: widget.supplierName,
        productImage: widget.currentProduct.image,
        totalPrice: total.toString());
    int theTotal = widget.cart.total;
    List<OrderItem> toAdd= [];
    if (widget.cart.orderDetails.length > 0){
      for (var item in widget.cart.orderDetails){
        if (item.productName == orderItemInstance.productName){
          item.productQuantity += orderItemInstance.productQuantity;
          item.totalPrice += orderItemInstance.totalPrice;
          theTotal += addedAmount;
        } else {
          toAdd.add(orderItemInstance);
          theTotal += addedAmount;
        }
      }
    } else {
      theTotal += addedAmount;
//      widget.cart.orderDetails.add(orderItemInstance);
      toAdd.add(orderItemInstance);
    }
    widget.cart.total += total;
    toAdd.forEach((element) {
      widget.cart.orderDetails.add(element);
    });
    setState(() {});
  }

