How to filter a list in Flutter?

Viewed 775
//list

[
    {
        "url": "https://",
        "name": "A",
        "tags": ["tag1"]
    },
    {
        "url": "https://",
        "name": "B",
        "tags": ["tag1", "tag2"]
    }
]
tag = 'tag2';
list2 = list.where((map)=>tag.contains(map["tags"])).toList();
print(list2);
//result: []

I'd like to make a new list that every item contains tag2 but code is not working.

list2 = list.where((map)=>tag.contains(map["tags"])).toList(); // not working...
list2 = list.where((map)=>tag.contains(map["tags"][1])).toList(); // It's working but "tags": ["tag2", "tag1"] is not working.

Anyone know how can do it? Thanks! (and sorry for my poor english...)

2 Answers

try

list2 = list.where((map)=>map["tags"].contains(tag)).toList();

list2 = list.where((map)=>map["tags"].contains(tag)).toList();

Related