Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: filter lists with maps

Tags:

flutter

dart

LIST 1: I have a list with user ids: (selected users)

['1234', '3455', '2330', '1111']

LIST 2: I have a second list with the full profile of the users (map): (all users)

[{"id": '1234', "name": "username"}, {"id": '3455' .... ]

LIST 3: I would like to have a third list that contains all selected users: returning all users of LIST 2 that have an id of LIST 1.

I tried this...

 list1.forEach((element) =>
         list3.add(list2.where((data) => data['id'] == element)));

But that doesn't work...

error: The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to the parameter type 'Map<String, dynamic>'. 

What am I doing wrong?

Thanks in advance!

like image 596
Karel Debedts Avatar asked Nov 02 '19 12:11

Karel Debedts


1 Answers

You can use list3 = list2.where((map)=>list1.contains(map["id"])).toList() for create list3

And the reason of issue is your list3 needs a map, but your list2.where(..) code is returns List[map, map, map].

like image 81
Ibrahim Karahan Avatar answered Nov 03 '22 00:11

Ibrahim Karahan