I have array of data returning from local JSON file and I need to filter data base on items specific value.
Data sample
[
{
"verse": ".........",
"topic": "A"
},
{
"verse": ".........",
"topic": "A"
},
{
"verse": ".........",
"topic": "A"
},
{
"verse": ".........",
"topic": "A"
},
{
"verse": ".........",
"topic": "B"
},
{
"verse": ".........",
"topic": "B"
},
..........
]
Here for instance I want to get data with topic A
only.
Code
readDataBaseData() async {
String data =
await DefaultAssetBundle.of(context).loadString("assets/db/my_file.json");
final jsonResult = jsonDecode(data);
List items = jsonResult['ID'];
// need that filter here (before return).
return items; // currently returns sample data above
}
Ideas?
CodePudding user response:
You have to iterate over your list, a one-liner to remove all entries where the topic is not equal to A
would be:
items.removeWhere((item) => item["topic"] != "A");
You can also use retainWhere
:
items.retainWhere((item) => item["topic"] == "A");