To slice a list in Dart, you can use the sublist method provided by the List class.
In this example,
numbers
with integer values.sublist
method on the list object numbers
to create a new list from index 1 to index 4.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<int> slicedNumbers = numbers.sublist(1, 4);
print('Sliced list: \$slicedNumbers');
}
Sliced list: [2, 3, 4]
In this example,
words
with string values.sublist
method on the list object words
to create a new list from index 1 to index 4.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
List<String> slicedWords = words.sublist(1, 4);
print('Sliced list: \$slicedWords');
}
Sliced list: [banana, cherry, date]
In this tutorial, we learned How to Slice a List in Dart language with well detailed examples.