Flutter 颤振中的jsonData滤波

Flutter 颤振中的jsonData滤波,flutter,dart,Flutter,Dart,我有一个json响应,如下所示。从响应中,我需要过滤从属关系deptId=1的数据。json响应是: { "success": true, "data": [{ "id": 4, "userId": 11, "active": true, "affiliations": [{ "id": 3, "deptI

我有一个json响应,如下所示。从响应中,我需要过滤从属关系deptId=1的数据。json响应是:

{
    "success": true,
    "data": [{
            "id": 4,
            "userId": 11,
            "active": true,
            "affiliations": [{
                    "id": 3,
                    "deptId": 1,
                    "active": true,
                    "createdAt": "2020-04-28T05:28:12.065Z",
                    "updatedAt": "2020-04-28T05:28:12.065Z"
                },
                {
                    "id": 4,
                    "deptId": 2,
                    "active": true,
                    "createdAt": "2020-04-28T05:28:12.065Z",
                    "updatedAt": "2020-04-28T05:28:12.065Z"
                }
            ]
        },
        {
            "id": 5,
            "userId": 6,
            "active": true,
            "affiliations": [{
                "id": 5,
                "deptId": 1,
                "active": true,
                "createdAt": "2020-04-28T05:28:12.065Z",
                "updatedAt": "2020-04-28T05:28:12.065Z"
            }]
        },
        {
            "id": 7,
            "userId": 11,
            "active": true,
            "affiliations": [{
                "id": 6,
                "deptId": 1,
                "active": true,
                "createdAt": "2020-04-28T05:28:12.065Z",
                "updatedAt": "2020-04-28T05:28:12.065Z"
            }]
        }
    ]
}
要根据活动状态进行过滤,我将执行以下操作:

snapshot.data['data'].where((data)=>data['active']==true)).toList()

上述问题的语法是什么?

我不知道你的应用程序结构如何,但我通常使用if-else只显示我想显示的数据,例如,如果我使用listview生成器,我会在容器或文本小部件之前添加类似的内容

snapshot.data[index].affiliations.deptId == 1 ?
Text(snapshot.data[index].affiliations.id.toString()) 
: Text("") 
或者,您可以从数据中创建一个新列表。举个例子:

var filteredList = snapshot.data.where((i) => i.affiliations.deptId == 1).toList();

为了便于参考,我将在这里编写两种工作方法,用于过滤列表,而不使用For循环

import 'dart:convert';

final String body = """
  {
    "success": true,
    "data": [{
            "id": 4,
            "userId": 11,
            "active": true,
            "affiliations": [{
                    "id": 3,
                    "deptId": 1,
                    "active": true,
                    "createdAt": "2020-04-28T05:28:12.065Z",
                    "updatedAt": "2020-04-28T05:28:12.065Z"
                },
                {
                    "id": 4,
                    "deptId": 2,
                    "active": true,
                    "createdAt": "2020-04-28T05:28:12.065Z",
                    "updatedAt": "2020-04-28T05:28:12.065Z"
                }
            ]
        },
        {
            "id": 5,
            "userId": 6,
            "active": true,
            "affiliations": [{
                "id": 5,
                "deptId": 1,
                "active": true,
                "createdAt": "2020-04-28T05:28:12.065Z",
                "updatedAt": "2020-04-28T05:28:12.065Z"
            }]
        },
        {
            "id": 7,
            "userId": 11,
            "active": true,
            "affiliations": [{
                "id": 6,
                "deptId": 1,
                "active": true,
                "createdAt": "2020-04-28T05:28:12.065Z",
                "updatedAt": "2020-04-28T05:28:12.065Z"
            }]
        }
    ]
}
  """;

void main() {
  Map snap = jsonDecode(body);

  List data = snap["data"];

  /*
  Find all items that have at least 1 subitem with deptId==1
  Output is: 11, 6, 11
  */
  List list1 = data
      .where((d) =>
          d["active"] == true &&
          d["affiliations"].any((dd) => dd["deptId"] == 1))
      .toList();
  list1.forEach((el) => print("${el["userId"]}"));

  /*
  Find all subitems which have deptId==1
  Output is: 3, 5, 6
  */
  List list2 = data
      .where((d) => d["active"] == true)
      .toList()
      .map((d) => d["affiliations"]
          .map((aff) => aff)
          .toList()
          .where((dd) => dd["deptId"] == 1)
          .toList())
      .toList();
  list2.forEach((el) => print(el[0]["id"]));

}

希望能有所帮助。

在这种情况下,我不使用listview。所以我没有这些项目的索引。我可以使用for循环,比较并添加到列表中,但我正在寻找一个更干净的解决方案。比如使用snapshot.data.data.where子句。此解决方案不起作用,因为从属关系本身是一个数组,如果不向从属关系传递索引,它就不起作用。我认为解决此问题的唯一方法是在服务器端进行筛选,例如类似firebase或firestore equalTo查询方法的方法,但我认为这不适合您的情况,或者你可以在未来的函数中尝试一个循环,…你好,你能更好地解释一下你期望得到的结果项吗?所有至少拥有deptId==1或其他分支机构的用户?谢谢,是的。我就是这么说的。我需要联系部门1的数据。但别客气。我在服务器端做了修改[添加了一个包含所有附属部门id的关键部门,并检查data.depts是否包含1]。我很高兴你解决了它;如果要选中此选项:List List=snap[data]。其中d{return d[active]==true&&d[affiliations]。anydd=>dd[deptId]==1;}.toList;forEachel=>print${el[userId]};谢谢你的解决方案。但我最近才发现了这一点,并为其他同样的问题实现了精确的语法。我正要在这里发布这个答案。谢谢你的解决方案,但经过一些尝试和错误后,我已经找到了答案。它现在正在工作。但现在,其他有同样问题的人能够轻松解决,这是件好事。