Flutter更新列表内的地图值

时间:2020-08-08 15:58:53

标签: flutter

大家好,我正在尝试更新列表中的地图值(如果该值已经存在),那么它应该只更新数量。问题在于该代码仅适用于第一个条目,而不适用于其余条目。

这是我的代码。

用于(项目中的可变地图){ print(countItem.indexOf(_productIndex));

    if (map['\'id\''] == '\'$_productIndex\'') {
      var toRemove = countItem.indexOf(_productIndex);
      items.removeAt(toRemove);
      items.add({
        '\'totalQuantity\'': count,
        '\'type\'': '\'unit\'',
        '\'id\'': '\'$_productIndex\'',
        '\'price\'': count * int.parse(_productPrice),
        '\'name\'': '\'$_productName\'',
      });
    } else {
      items.add({
        '\'totalQuantity\'': count,
        '\'type\'': '\'unit\'',
        '\'id\'': '\'$_productIndex\'',
        '\'price\'': int.parse(_productPrice),
        '\'name\'': '\'$_productName\'',
      });
    }

    break;
  }

1 个答案:

答案 0 :(得分:0)

您将indexOf用于错误的变量。将其从_productIndex更改为地图。

for (var map in items) { 
   print(countItem.indexOf(map));
   ...