The syntax of List.skipWhile() method is:
Iterable<E> skipWhile(bool test(E value))
This skipWhile() method of List creates an Iterable
that skips leading elements while test
is satisfied.
Parameter | Optional/Required | Description |
---|---|---|
test | required | the function returning true to continue skipping elements or false to stop |
List.skipWhile() returns value of type Iterable<E>
.
In this example,
numbers
containing [1, 2, 3, 4, 5]
.skipWhile()
method on numbers
, skipping elements while they are less than 3.void main() {
var numbers = [1, 2, 3, 4, 5];
var skipped = numbers.skipWhile((number) => number < 3);
print('Skipped elements: $skipped');
}
Skipped elements: (3, 4, 5)
In this example,
characters
containing ['a', 'b', 'c', 'd', 'e']
.skipWhile()
method on characters
, skipping elements until 'c' is found.void main() {
var characters = ['a', 'b', 'c', 'd', 'e'];
var skipped = characters.skipWhile((char) => char != 'c');
print('Skipped elements: $skipped');
}
Skipped elements: (c, d, e)
In this example,
words
containing ['apple', 'banana', 'cherry', 'date']
.skipWhile()
method on words
, skipping words with a length less than 6.void main() {
var words = ['apple', 'banana', 'cherry', 'date'];
var skipped = words.skipWhile((word) => word.length < 6);
print('Skipped elements: $skipped');
}
Skipped elements: (banana, cherry, date)
In this Dart tutorial, we learned about skipWhile() method of List: the syntax and few working examples with output and detailed explanation for each example.