list - 如何从Dart Flutter中的键或键值对获取索引

标签 list flutter dictionary dart

我有一个键值对列表
我想从类似键的索引中搜索索引(在列表中的索引== 0的列表中获取索引),然后从该索引中我想更改值。
或将值更改为key == 1

void main() {
  var listAnswers = [];
  var keyPair = {
    'Key': 0,
    'value': false,
  };

  listAnswers.add(keyPair);

  keyPair = {
    'Key': 1,
    'value': 1,
  };

  listAnswers.add(keyPair);

  keyPair = {
    'Key': 2,
    'value': DateTime.now(),
  };

  listAnswers.add(keyPair);

  print(listAnswers);

  var index = listAnswers.getIndex('key', 0)
  // got the index 1 (where key = 0, and value = false)

  listAnswers[i].value = true; // then I want to do something like this.

}

最佳答案

  var index = listAnswers.indexWhere((pair) => pair['Key'] == 0);
      print(index);
      
     listAnswers[index]['value'] = true;

关于list - 如何从Dart Flutter中的键或键值对获取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63995677/

相关文章:

C#比较两个对象列表列表

flutter - FadeInImage onFocusChange 重新生成

flutter - 在flutter中添加位置插件时出错?

angularjs - 如何在 AngularJS 中按值对字典进行排序

java - hasNext 跳过 HashMap 中的第一个值

python - 根据其他列和字典创建一个新列

java - 删除一个对象并在 TreeSet 中添加另一个对象

python - 从字符串中提取单词,删除标点符号并返回带有分隔单词的列表

r - 转置嵌套列表

flutter - FutureBuilder因Flutter中的TextField而重建