srKotlin
srKotlin

Reputation: 31

Best method to set a variable to true if all values inside a List of bool are true

I have a List of model Test, where Test includes a boolean property called chosen, what would be the best way of setting a variable to true if all entries in the List's chosen property are all true?

At the moment, I have a for loop set up that runs through each entry in the list and checks if chosen is true. If any chosen are false, then the checker variable needs to be false:

for (var i = 0; i < List.length; i++)
    if List[i].chosen == false
       checker = false;

Is there a better method to do this with List iterations?

Upvotes: 3

Views: 1868

Answers (1)

Claudio Castro
Claudio Castro

Reputation: 1569

You can use every()

 checker = testModel.every((e) => e.chosen); 

Upvotes: 5

Related Questions