Home > Enterprise >  Get all elements after a certain index in Dart
Get all elements after a certain index in Dart

Time:12-10

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?

CodePudding user response:

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); //[]
  •  Tags:  
  • dart
  • Related