Home > Net >  How to remove the last three elements in a list using Dart?
How to remove the last three elements in a list using Dart?

Time:06-22

if you have list like this ...

      List<String> data = ['1', '2', '3', '4', '5',..... n];

how can you remove the last three element knowing that you don't know the length of the list, i tried to use removeRange() but it didn't go well.

CodePudding user response:

Try below code

void main() {
  List data  = ['1', '2', '3', '4', '5'];
  data .length = data.length - 3;//put your how many list item you want to remove eg.1,2,3,...n
  print(data);
}

Result-> [1, 2]

CodePudding user response:

you can use removeRange like this:

data.removeRange(data.length - 3, data.length);

  • Related