Noah_J.C_Lee
Noah_J.C_Lee

Reputation: 177

How to get list of boolean by comparing two list flutter

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

Upvotes: 0

Views: 576

Answers (2)

Tomer Ariel
Tomer Ariel

Reputation: 1537

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

Upvotes: 1

Ivo
Ivo

Reputation: 23209

You can do

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

Upvotes: 0

Related Questions