To get a sublist in Dart, you can use the sublist
method of the List
class.
In this example,
myList
with some elements.sublist
method to get a sublist from myList
. This method takes two parameters: the starting index (inclusive) and the ending index (exclusive).sublist
method returns a new list containing the specified range of elements.void main() {
List<int> myList = [1, 2, 3, 4, 5];
List<int> subList = myList.sublist(1, 4);
print('Sublist: \$subList');
}
Sublist: [2, 3, 4]
In this example,
myList
with some elements.sublist
method to get a sublist from myList
by specifying different start and end indices.sublist
method returns a new list containing the specified range of elements.void main() {
List<int> myList = [10, 20, 30, 40, 50, 60];
List<int> subList = myList.sublist(2, 5);
print('Sublist: \$subList');
}
Sublist: [30, 40, 50]
In this tutorial, we learned How to get Sub List in Dart language with well detailed examples.