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

Dynamically create a list of variables gives error RangeError (index): Invalid value: Valid value range is empty: 0

I am trying to create two lists with dynamic variables but I get the following error:

RangeError (index): Invalid value: Valid value range is empty: 0

List<bool> variableOne= [];
List<bool> variableTwo= [];

for (var i = 0; i < 3; i++) {
   variableOne[i] = false;
   variableTwo[i] = true;
}

The output that I want is:

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

variableOne[0] = false;
variableTwo[0] = true;
variableOne[1] = false;
variableTwo[1] = true;
variableOne[2] = false;
variableTwo[2] = true;

Any idea what is wrong?

>Solution :

When you create empty list, the list does not have any elements that you can’t access elements by using index. it means that you can’t assign a value to a index of a list that is not existed yet.

you can use add method to add values to your list and then the indexes will be created:

List<bool> variableOne = [];
List<bool> variableTwo = [];

for (var i = 0; i < 3; i++) {
   variableOne.add(false);
   variableTwo.add(true);
}

happy coding…

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