flutter - 如何确定 map 列表是否包含特定的键值?

标签 flutter dart

我无法理解如何通过键检查 map 列表是否包含值。以下是我拥有的数据结构。

[
  {
    id: 1,
    bookTxt: Hereissomebooktext.,
    bookAuth: Charles
  },
  {
    id: 3,
    bookTxt: Hereissomemorebooktext.,
    bookAuth: Roger
  },
  {
    id: 6,
    bookTxt: Hereissomeevenmorebooktext.,
    bookAuth: Matt
  }
]

我想写一些简单的东西或一个函数来查看这个 map 列表是否包含某个“id”。我知道 List 有 Contains 方法,但在我的例子中,我必须在 map 列表中找到一个值。

例如,如果我想查看上面的 map 列表是否包含 3id,我将如何访问它?

最佳答案

直接检查

if (list[0].containsKey("id")) {
  if (list[0]["id"] == 3) {
    // your list of map contains key "id" which has value 3
  }
}

对于间接方式,您需要像这样遍历循环:

for (var map in list) {
  if (map?.containsKey("id") ?? false) {
    if (map!["id"] == 3) {
      // your list of map contains key "id" which has value 3
    }
  }
}

关于flutter - 如何确定 map 列表是否包含特定的键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544033/

相关文章:

flutter - 如何使导航底部栏透明

unit-testing - 如何防止从使用dart中的ExpectAsync的单元测试进行?

firebase - Flutter - 从 firestore 循环获取数据

firebase - 我可以在 Firestore 搜索中过滤多个字段吗?

flutter : How can i create this circle avatar that's half out of the container?

dart - ListView.builder 不显示任何内容 - Flutter

flutter : How to change format dateTime with mounth in String?

listview - 列中展开的 ListView 占用额外空间

android - 如何在 Flutter App 中处理 onPause/onResume?

firebase - E/FirebaseInstanceId( 5789) : Topic sync or token retrieval failed on hard failure exceptions: AUTHENTICATION_FAILED. 不会重试操作