Cenk YAGMUR
Cenk YAGMUR

Reputation: 3451

Dart how do I find the index of true value in the list

I have list List<bool> _selections = [false, true, false]; and this list may change only one can be true

How do I know which index is true ?

Upvotes: 1

Views: 1411

Answers (3)

hewa jalal
hewa jalal

Reputation: 963

while the above answer are correct and simpler you can also use a good old foreach loop like this:

for (var elements in _selections) {
    if (elements == true) {
      print(elements);
    }
  }

Upvotes: 2

mducc
mducc

Reputation: 743

You can try indexWhere() method.

_selections.indexWhere((ele) => ele);

See more

Upvotes: 2

Uj Corb
Uj Corb

Reputation: 2159

you can use indexWhere

_selections.indexWhere((value) => value)

Upvotes: 3

Related Questions