Dart Set skipWhile()
Syntax & Examples
Set.skipWhile() method
The `skipWhile` method in Dart returns an Iterable that skips leading elements of the set while the provided test function returns true.
Syntax of Set.skipWhile()
The syntax of Set.skipWhile() method is:
Iterable<E> skipWhile(bool test(E value))
This skipWhile() method of Set returns an Iterable that skips leading elements while test is satisfied.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
test | required | The function used to test elements of the set. |
Return Type
Set.skipWhile() returns value of type Iterable<E>
.
✐ Examples
1 Skip integers less than 3
In this example,
- We create a Set
set
containing integers. - We use the
skipWhile()
method with a test function that checks if each element is less than 3. - We print the Iterable containing elements after skipping to standard output.
Dart Program
void main() {
Set<int> set = {1, 2, 3, 4, 5};
Iterable<int> skippedSet = set.skipWhile((element) => element < 3);
print('Skipped set elements: $skippedSet');
}
Output
Skipped set elements: (3, 4, 5)
2 Skip strings with length less than 6
In this example,
- We create a Set
set
containing strings. - We use the
skipWhile()
method with a test function that checks if the length of each string is less than 6. - We print the Iterable containing elements after skipping to standard output.
Dart Program
void main() {
Set<String> set = {'apple', 'banana', 'orange'};
Iterable<String> skippedSet = set.skipWhile((element) => element.length < 6);
print('Skipped set elements: $skippedSet');
}
Output
Skipped set elements: (banana, orange)
Summary
In this Dart tutorial, we learned about skipWhile() method of Set: the syntax and few working examples with output and detailed explanation for each example.