Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 706
Lukas Süsslin Avatar asked Nov 28 '16 16:11

Lukas Süsslin


People also ask

How do you assign a Dart list length?

If you want to increase it then you can do this num. addAll([1,1,1,1,1,1,1]) or num. addAll(List<int>. generate(20, (i) => 0)) .

How do you make a list growable in darts?

As stated in the comments, you need to create each list separately and add them to them list of lists. The default growable list, as returned by new List() or [], keeps an internal buffer, and grows that buffer when necessary.


1 Answers

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.

like image 113
Harry Terkelsen Avatar answered Oct 05 '22 05:10

Harry Terkelsen