【问题标题】:Flutter how to link arrays by idsFlutter 如何通过 id 链接数组
【发布时间】:2022-08-07 15:49:24
【问题描述】:

我有这样的数据

 var sendlocal = [
  {
    \"firstName\": \"tree\",
    \"lastName\": \"tree\",
    \"relativeEmail\": \"tree@gmail.com\",
    \"relativeType\": 0,
    \"subid\": 1,
    \"subRelatives\": [
      {
        \"firstName\": \"julia2\",
        \"lastName\": \"Michle\",
        \"relativeEmail\": \"test@hotmail.com3\",
        \"relativeType\": 2,
        \"subid\": 2,
        \"subRelatives\": [
          {
            \"firstName\": \"john\",
            \"lastName\": \"bravo\",
            \"relativeEmail\": \"johny@gmail.com\",
            \"relativeType\": 1,
            \"subRelatives\": [],
            \"subid\": 3,
          },
          {
            \"firstName\": \"simith\",
            \"lastName\": \"bravo\",
            \"relativeEmail\": \"johny@gmail.com\",
            \"relativeType\": 1,
            \"subRelatives\": [],
            \"subid\": 4,
          },
        ],
      },
      {
        \"firstName\": \"julia3\",
        \"lastName\": \"Michle\",
        \"relativeEmail\": \"test3@hotmail.com\",
        \"relativeType\": 2,
        \"subRelatives\": [],
        \"subid\": 5,
      },
    ],
  },
];

根据下面的答案(谢谢)我创建了一个这样的函数

getIndexFromNestedList(List mapValue) { 如果(地图值!= null){ for(mapValue 中的 var 关系){ 打印(关系[\'subid\']); 打印(关系[\'相对电子邮件\']);

    if (relation[\'subRelatives\'] != null) {
      for (var subRelation in relation[\'subRelatives\']) {
        print({relation[\'subid\'], subRelation[\'subid\']});
        // graph.addEdge(relation[\'subid\'], subRelation[\'subid\']); //like this
        Future.delayed(Duration(milliseconds: 1), () {
          getIndexFromNestedList(relation[\'subRelatives\']);
        });
      }
    }
  }
}

}

并像这样传递数据

var check = getIndexFromNestedList(relatives);

我得到这样的回应

flutter: {1, 2}
flutter: {1, 3}
4flutter: {2, null}

期望的是 {2,3} {2,4} 也是,但它显示为 null 不知道为什么它会去亲戚。

    标签: flutter dart


    【解决方案1】:

    您可以使用嵌套的 for 循环来获取这些值,如下所示,并将循环重新发送到方法以获取嵌套值

    getIndexFromNestedList(List<dynamic> mapValue) {
      for (var relation in mapValue) {
        if (relation['sub'] != null) {
          for (var subRelation in relation['sub']) {
            print({relation['id'], subRelation['id']});//you can add it directly to the map here.
            graph.addEdge(relation['id'], subRelation['id']);//like this
            Future.delayed(Duration(milliseconds:1), (){//future is added to finish the first loop then to enter into the inner loops.
              getIndexFromNestedList(relation['sub']);
            });
          }
        }
      }
    }
    
    //OUTPUT
    {1, 2}
    {1, 6}
    {2, 3}
    {2, 5}
    {2, 3}
    {2, 5}
    {3, 4}
    {3, 4}
    {3, 4}
    {3, 4}
    

    您可以使用此方法将值设置到图中

    getIndexFromNestedList(reList);
    

    编辑对于当前结构,您可以在访问其子项之前检查第一个值是否不为空,如下所示

    getIndexFromNestedList(List<dynamic> mapValue) {
      if (mapValue != null) {
        for (var relation in mapValue) {
          print(relation['subid']);
          print(relation['relativeEmail']);
    
          if (relation['subRelatives'] != null) {
            for (var subRelation in relation['subRelatives']) {
              print({relation['subid'], subRelation['subid']});
              // graph.addEdge(relation['subid'], subRelation['subid']); //like this
              Future.delayed(Duration(milliseconds: 1), () {
                getIndexFromNestedList(relation['subRelatives']);
              });
            }
          }
        }
      }
    }
    

    【讨论】:

    • 感谢您的回答,它有帮助,但我已经用我的真实数据编辑了问题,不知道为什么它在子阵列上的 1,2 到 1,6 之后卡住了。如果你能在这方面提供帮助。
    • 打印(关系['relativeEmail'])。并非所有数据列表都具有此值.. 将其放入 try catch
    • 但只是打印我们正在使用 subid 的 relativeEmail 并且所有列表都有每个 subid
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2012-07-24
    相关资源
    最近更新 更多