Home > Software design >  How to get a specific map out of a list of maps that has a bigger than or smaller than value than th
How to get a specific map out of a list of maps that has a bigger than or smaller than value than th

Time:08-30

I'm working on a app where I retrieve json data via http, and I have made the listview.builder and all of that. Now I want to make a filter button to show the lists that only have values below a certain integer. for example: my list of maps goes something like this

[
  {
    "name": "jess",
    "age": "28",
    "job": "doctor"
  },
  {
    "name": "jack",
    "age": "30",
    "job": "jobless"
  },
  {
    "name": "john",
    "age": "24",
    "job": "doctor"
  },
  {
    "name": "sara",
    "age": "23",
    "job": "teacher"
  }...etc
]

Now I want to press that filter button and in my listview show only those that are below or above the age of 25.

CodePudding user response:

You can map over it and add your "filter" inside the condition. Since you save age as String you have to parse it to an int:

 var filteredList = myMapList.map((e){
    int? parsedAge = int.tryParse(e["age"]!);
    if(parsedAge != null && parsedAge >= 25){
      return e;
    }
   }).toList();

CodePudding user response:

I got the answer and it was there all the time I just didn't thought about that much. In case anyone faces the same issue, here's how it worked for me.

list filteredList = peopleDetailsList.where((element) => int.parse(element['age'] < 25).toList();
  • Related