Home > other >  How to get list of boolean by comparing two list flutter
How to get list of boolean by comparing two list flutter

Time:09-13

Currently I am build a widget using Listview.builder

I needs two list. The first list what i need is first List is 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 what i want is

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

but the results what i've tried returns only True it self

CodePudding user response:

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].

CodePudding user response:

You can do

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