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

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

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.

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

>Solution :

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]

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