簡體   English   中英

如何替換列表中的 object<map> 在 Flutter 的同一索引位置</map>

[英]how to replace object in List<Map> at same index location in Flutter

所以可以說我有一個這樣的對象列表:

List<Map<String, dynamic>> list = [{'a': 'red', 'b': 'blue'}, {'a': 'yellow', 'b': 'orange'}, {'a': 'green', 'b': 'purple'}];

假設每個 a 和 b 值都是唯一的,我將如何找到其中a === yellowobject並將 object 替換為新的 object,例如{'a': 'brown', 'b': 'white'} ,原 object 列表中的索引位置

編輯我忘了提,我需要先刪除 object 來操作數據,然后將其添加回同一位置。

嘗試這個:

  var item = list.firstWhere((i) => i["a"] == 'yellow'); // getting the item
  var index = list.indexOf(item); // Item index
  list[index] = {'a': 'brown', 'b': 'white'}; // replace item at the index

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM