[Solved] How to set all data of List<bool> to be false with some function

Question

Asked by Eko M on January 13, 2022 (source).

Anyone know how to set all data of List<bool> to be false? Here is example:

List<bool> data = [true, false, true];

data = [false, false, false];
// output data = [false, false, false]

and there is my code to convert it, and i know that we can do with looping. But here, i want to do with some function, which is i was tried with data.every((element) => element = false); but, that is not working.

Thanks in advance

Answer

Question answered by h8moss (source).

There are many ways to do something like this, here are a couple of them:

First, use the map method:

data = data.map<bool>((v) => false).toList();

The map method transforms every item on a list, we are using it like you wanted to use every

Second, use the filled method:

data = List.filled(data.length, false, growable: true);

The filled method makes a new list given a length and a value, in our case the length is the previous list length and the value is false.

ARRAYS DART FLUTTER
SHARE: