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

Get all elements after a certain index in Dart

I want to retrieve all elements after a starting index from a list.

List myList = [1,2,3,4,5];
print([2:]) // returns [3,4,5]

If It’s out of range I want to return an empty list.

How do I do that in dart?

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 :

This is what the skip method is for. If you look at the documentation for it, it has the behavior you desire.

List<int> myList = [1,2,3,4,5];
List<int> newList = myList.skip(2).toList();
print(newList); //[3, 4, 5]
List<int> myList = [1,2,3,4,5];
List<int> newList = myList.skip(5).toList();
print(newList); //[]
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