Umaiz Khan
Umaiz Khan

Reputation: 1227

Flutter how to link arrays by ids

I have data like this

 var sendlocal = [
  {
    "firstName": "tree",
    "lastName": "tree",
    "relativeEmail": "[email protected]",
    "relativeType": 0,
    "subid": 1,
    "subRelatives": [
      {
        "firstName": "julia2",
        "lastName": "Michle",
        "relativeEmail": "[email protected]",
        "relativeType": 2,
        "subid": 2,
        "subRelatives": [
          {
            "firstName": "john",
            "lastName": "bravo",
            "relativeEmail": "[email protected]",
            "relativeType": 1,
            "subRelatives": [],
            "subid": 3,
          },
          {
            "firstName": "simith",
            "lastName": "bravo",
            "relativeEmail": "[email protected]",
            "relativeType": 1,
            "subRelatives": [],
            "subid": 4,
          },
        ],
      },
      {
        "firstName": "julia3",
        "lastName": "Michle",
        "relativeEmail": "[email protected]",
        "relativeType": 2,
        "subRelatives": [],
        "subid": 5,
      },
    ],
  },
];

And as per below answer(Thanks) I create a function like this

  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']);
            });
          }
        }
      }
    }
  }

and pass data like this

var check = getIndexFromNestedList(relatives);

And I getting responses like this

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

What Expected is {2,3} {2,4} and so on if sub relative also have sub relative. But it's showing null don't know why when it's going to sub relatives.

Upvotes: 2

Views: 220

Answers (2)

Kaushik Chandru
Kaushik Chandru

Reputation: 17772

You can use a nested for loop to get those values like the following and resend the loop to the method to get the nested values

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}

you can use this method to set the values into the graph

getIndexFromNestedList(reList);

EDIT for the current structure you can check if the first value is not null before you access its children like the following

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']);
          });
        }
      }
    }
  }
}

Edit on 7AUG

In the question again you missed to check if its a null value and if length is greater than 0. also you are passing relatives to the method.. The list name wassendlocal

Here is the updated method with length check too

  getIndexFromNestedList(List mapValue) {
    if (mapValue != null) {
      for (int i = 0; i < mapValue.length; i++) {
        for (int j = 0; j < mapValue[i]['subRelatives'].length; j++) {
          Map subList = mapValue[i]['subRelatives'][j];
          print({mapValue[i]['subid'], subList['subid']});
          if (subList['subRelatives'].length > 0) {
            Future.delayed(Duration(milliseconds: 10), () {
              getIndexFromNestedList(mapValue[i]['subRelatives']);
            });
          }
        }
      }
    }
  }

//Output
flutter: {1, 2}
flutter: {1, 5}
flutter: {2, 3}
flutter: {2, 4}

Tested this multiple times and removed all duplicates too.

Upvotes: 2

Shirsh Shukla
Shirsh Shukla

Reputation: 5973

I got your answer as per your requirement, maybe this helpful for you

void main(){
  var check =getIndexFromList(sendlocal);
}

getIndexFromList(List sendlocal) {
    if (sendlocal != null) {
      for (var relation in sendlocal) {
        print('subid:- ${relation['subid']}');
        print('relativeEmail:- ${relation['relativeEmail']}');

        if (relation['subRelatives'] != null && relation['subRelatives'].length > 0) {
          for (var subRelation in relation['subRelatives']) {
            
            //its your required response
            print({relation['subid'], subRelation['subid']});
            
            Future.delayed(Duration(milliseconds: 2), () {
              getIndexFromList(relation['subRelatives']);
            });
          }
        }
      }
    }
  }


var sendlocal = [
  {
    "firstName": "tree",
    "lastName": "tree",
    "relativeEmail": "[email protected]",
    "relativeType": 0,
    "subid": 1,
    "subRelatives": [
      {
        "firstName": "julia2",
        "lastName": "Michle",
        "relativeEmail": "[email protected]",
        "relativeType": 2,
        "subid": 2,
        "subRelatives": [
          {
            "firstName": "john",
            "lastName": "bravo",
            "relativeEmail": "[email protected]",
            "relativeType": 1,
            "subRelatives": [],
            "subid": 3,
          },
          {
            "firstName": "simith",
            "lastName": "bravo",
            "relativeEmail": "[email protected]",
            "relativeType": 1,
            "subRelatives": [],
            "subid": 4,
          },
        ],
      },
      {
        "firstName": "julia3",
        "lastName": "Michle",
        "relativeEmail": "[email protected]",
        "relativeType": 2,
        "subRelatives": [],
        "subid": 5,
      },
    ],
  },
]; 

The response is something like that,

 subid:- 1
    relativeEmail:- [email protected]
    {1, 2}
    {1, 5}
    subid:- 2
    relativeEmail:- [email protected]
    {2, 3}
    {2, 4}
    subid:- 5
    relativeEmail:- [email protected]
    subid:- 2
    relativeEmail:- [email protected]
    {2, 3}
    {2, 4}
    subid:- 5
    .............
    .............

Upvotes: 1

Related Questions