Home > Mobile >  How to access map value of nested list
How to access map value of nested list

Time:10-18

For example, I have a list of data like this List data = [[{"number": 1, "page": 1, "Text": "a"}],[{"number": 2, "page": 2, "Text": "b"}],[{"number": 3, "page": 3, "Text": "c"}],[{"number": 4, "page": 4, "Text": "d"}], [{"number": 5, "page": 5, "Text": "e"}],[{"number": 6, "page": 6, "Text": "f"}],[{"number": 7, "page": 7, "Text": "g"}]]; and I want to where((e) => e['page'] == 1) how can I do something like this, At first i tried data.map((e) => e).toList().where((e) => e['page'] == 1) but it doesn't work, then I tried [for(int i = 0;i < data.map((e) => e).toList().length; i ) data[i].where((e) => e['page'] == 1)].toList() now it works [({number: 1, page: 1, Text: a}), (), (), (), (), (), ()] but not what I'm looking for, I don't want those blank spaces , can anyone help me how to get rid of it ( not [({number: 1, page: 1, Text: a}), (), (), (), (), (), ()][0] )

CodePudding user response:

Solution: Create a new list that will be the filtered version of data.

  List newList = [];
  for(int i = 0;i < data.length; i  ) {
    if (data[i][0]["page"] == 1) newList.add(data[i][0]);
    // Or if you really want a List of List
    //if (data[i][0]["page"] == 1) newList.add(data[i]);
  }
  
   print (newList);

Output is:

[[{number: 1, page: 1, Text: a}]]

Other Comment: Note that you are using a List of List in your data object where the inner list is useless, typically you want to have something like this [{"number": 1, "page": 1, "Text": "a"}], not like this[[{"number": 1, "page": 1, "Text": "a"}],

Let me know if the output is not exactly what you need and I will adjust the code.

  • Related