Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading