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:
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…