How to get list of boolean by comparing two list flutter

Viewed 34

Currently, I am building a widget using Listview.builder

I need two lists. The first list that I need is the first List A. which will count the length. And the second list is the list of booleans

so for example

this List A

A = [a, b, c, d ,e]

and this is List B

B =[c, e]

and the result that I want is

C = [false, false, true , false, true]

but the results that I've tried return only True itself

2 Answers

map should do the trick, but make sure to convert B to a set first since lookup is O(1).

List<String> A = ["a", "b", "c", "d", "e"];
Set<String> B = {"c", "e"};
List<bool> C = A.map((a) => B.contains(a)).toList();
print(C);

[false, false, true, false, true].

You can do

C = A.map((e) => B.contains(e));
Related