【问题标题】:Flutter return json where id = [31,34]颤振返回 json 其中 id = [31,34]
【发布时间】:2021-11-02 21:30:20
【问题描述】:

当我单击一个按钮时,我想获得一个新列表。下面的例子
我有这个列表:

List users = [
  {
    "id": 31,
    "name": "John",
  },
  {
    "id": 23,
    "name": "Sami",
  },
  {
    "id": 34,
    "name": "Leon",
  }
];

当搜索像[31,34]这样的id列表时

我需要返回这个结果

List users = [
  {
    "id": 31,
    "name": "John",
  },
  {
    "id": 34,
    "name": "Leon",
  }
];

【问题讨论】:

  • 然后使用 List.where 方法,例如:users.where((e) => [31, 34].contains(e['id']).toList() - 当然,您可以使用任何 ID 列表来代替 [31, 34]
  • 首先你必须得到一个包含所有项目ID的列表;

标签: arrays json flutter dart


【解决方案1】:

这是一个例子:

List users = [
  {
    "id": 31,
    "name": "John",
  },
  {
    "id": 23,
    "name": "Sami",
  },
  {
    "id": 34,
    "name": "Leon",
  }
];

List<searchIds> = [31, 23];

List<searchUsers> = [];

for (int i = 0; i < searchIds.length; i++) {
      searchUsers.add(users.where((x) => x["id"] == searchIds[i]).toList()[0]);
}

【讨论】:

    【解决方案2】:

    你可以像这样过滤掉它 var newList = users.where((e) =&gt; e['id']==31 || e['id']==34).toList();

    【讨论】:

    • 我有很多身份证。我不能那样做。
    • 然后用一个列表检查它并稍微改变方法,但首先你需要有一个List 和你想要搜索的ID var newList = users.where((e) =&gt; idsToSearch.contains(e['id'])).toList();
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2022-06-18
    • 2023-03-11
    • 2022-01-05
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多