Luke Muller Luke Muller - 4 days ago 5
Dart Question

What makes this a fixed-length list in Dart?

List<String> checkLength(List<String> input) {
if (input.length > 6) {
var tempOutput = input;
while (tempOutput.length > 6) {
var difference = (tempOutput.length/6).round() + 1;
for (int i = 0; i < tempOutput.length - 1; i + difference) {
tempOutput.removeAt(i); //Removing the value from the list
}
}
return tempOutput; //Return Updated list
} else {
return input;
}
}


I am trying to delete something out of a temporary list. Why does it not work? I do not see how it is fixed, in other problems I have solved, I used a similar approach and it worked (Even identical nearly)

Please note I am kind of new to Dart, so please forgive me this sort of question, but I couldn't figure out the solution.

Find the Code available in the Dart Link

Code in Dart

Answer

You can ensure that tempOutput is not a fixed-length list by initializing it as

var tempOutput = new List<String>.from(input);

thereby declaring tempOutput to be a mutable copy of input.

FYI it also looks like you have another bug in your program since you are doing i + difference in your for-loop update step but I think you want i += difference.

Comments