Dart List skipWhile()
Syntax & Examples


Syntax of List.skipWhile()

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.

Parameters

ParameterOptional/RequiredDescription
testrequiredthe function returning true to continue skipping elements or false to stop

Return Type

List.skipWhile() returns value of type Iterable<E>.



✐ Examples

1 Skip numbers less than 3

In this example,

  1. We create a list of numbers numbers containing [1, 2, 3, 4, 5].
  2. We use the skipWhile() method on numbers, skipping elements while they are less than 3.
  3. The skipped elements are printed to standard output.

Dart Program

void main() {
  var numbers = [1, 2, 3, 4, 5];
  var skipped = numbers.skipWhile((number) => number < 3);
  print('Skipped elements: $skipped');
}

Output

Skipped elements: (3, 4, 5)

2 Skip characters until 'c' is found

In this example,

  1. We create a list of characters characters containing ['a', 'b', 'c', 'd', 'e'].
  2. We use the skipWhile() method on characters, skipping elements until 'c' is found.
  3. The skipped elements are printed to standard output.

Dart Program

void main() {
  var characters = ['a', 'b', 'c', 'd', 'e'];
  var skipped = characters.skipWhile((char) => char != 'c');
  print('Skipped elements: $skipped');
}

Output

Skipped elements: (c, d, e)

3 Skip words with length less than 6

In this example,

  1. We create a list of words words containing ['apple', 'banana', 'cherry', 'date'].
  2. We use the skipWhile() method on words, skipping words with a length less than 6.
  3. The skipped elements are printed to standard output.

Dart Program

void main() {
  var words = ['apple', 'banana', 'cherry', 'date'];
  var skipped = words.skipWhile((word) => word.length < 6);
  print('Skipped elements: $skipped');
}

Output

Skipped elements: (banana, cherry, date)

Summary

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.