Dart List skip()
Syntax & Examples
Syntax of List.skip()
The syntax of List.skip() method is:
Iterable<E> skip(int count) This skip() method of List creates an Iterable that provides all but the first count elements.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
count | required | the number of elements to skip |
Return Type
List.skip() returns value of type Iterable<E>.
✐ Examples
1 Skip elements in a list of numbers
In this example,
- We create a list named
numberscontaining the integers[1, 2, 3, 4, 5]. - We then use the
skip()method to skip the first 2 elements of the list. - We print the resulting iterable to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> skippedNumbers = numbers.skip(2);
print(skippedNumbers);
}Output
(3, 4, 5)
2 Skip elements in a list of characters
In this example,
- We create a list named
characterscontaining the characters['a', 'b', 'c', 'd']. - We then use the
skip()method to skip the first element of the list. - We print the resulting iterable to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
Iterable<String> skippedCharacters = characters.skip(1);
print(skippedCharacters);
}Output
(b, c, d)
3 Skip elements in a list of words
In this example,
- We create a list named
wordscontaining the strings['apple', 'banana', 'cherry', 'date']. - We then use the
skip()method to skip the first 3 elements of the list. - We print the resulting iterable to standard output.
Dart Program
void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date'];
Iterable<String> skippedWords = words.skip(3);
print(skippedWords);
}Output
(date)
Summary
In this Dart tutorial, we learned about skip() method of List: the syntax and few working examples with output and detailed explanation for each example.