【问题标题】:Concurrent modification during iteration: Instance(length:5) of '_GrowableList'. Flutter迭代期间的并发修改:“_GrowableList”的实例(长度:5)。扑
【发布时间】:2020-11-19 23:35:39
【问题描述】:

我正在尝试更改 Flutter 中的列表,但它一直给我这个错误:

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

我已经搜索过,但没有找到适合这种情况的答案或解决方案

这里是我的代码:

 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(() {});
  }

基于 android studio 中的调试器。在从列表中添加或删除数据的行之一中捕获了错误 像这样:

      theTotal += addedAmount;
      widget.cart.orderDetails.add(orderItemInstance);

【问题讨论】:

  • 在迭代时不能修改正在迭代的集合。修改副本或迭代副本。

标签: android ios flutter dart


【解决方案1】:

我已经通过在完成loop 之后延迟add 部分来解决它。这是工作代码:

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(() {});
  }

【讨论】:

    猜你喜欢
    • 2014-04-20
    • 2020-04-21
    • 2021-03-20
    • 1970-01-01
    • 2014-09-04
    • 2020-05-08
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多