Dart Tutorials

Dart List followedBy()
Syntax & Examples

Syntax of List.followedBy()

The syntax of List.followedBy() method is:

 Iterable<E> followedBy(Iterable<E> other) 

This followedBy() method of List creates the lazy concatenation of this iterable and other.

Parameters

ParameterOptional/RequiredDescription
otherrequiredthe iterable to concatenate with this iterable

Return Type

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



✐ Examples

1 Combine two lists of numbers

In this example,

  1. We create two lists, numbers1 and numbers2, containing integers.
  2. We then use the followedBy() method on numbers1 to concatenate it with numbers2.
  3. The resulting iterable combined contains elements from both lists in sequence.
  4. We print the combined iterable to standard output.

Dart Program

void main() {
  var numbers1 = [1, 2, 3];
  var numbers2 = [4, 5, 6];
  var combined = numbers1.followedBy(numbers2);
  print(combined); // Output: (1, 2, 3, 4, 5, 6)
}

Output

(1, 2, 3, 4, 5, 6)

2 Combine two lists of characters

In this example,

  1. We create two lists, letters1 and letters2, containing characters.
  2. We then use the followedBy() method on letters1 to concatenate it with letters2.
  3. The resulting iterable combined contains characters from both lists in sequence.
  4. We print the combined iterable to standard output.

Dart Program

void main() {
  var letters1 = ['a', 'b', 'c'];
  var letters2 = ['x', 'y', 'z'];
  var combined = letters1.followedBy(letters2);
  print(combined); // Output: (a, b, c, x, y, z)
}

Output

(a, b, c, x, y, z)

3 Combine two lists of words

In this example,

  1. We create two lists, words1 and words2, containing strings.
  2. We then use the followedBy() method on words1 to concatenate it with words2.
  3. The resulting iterable combined contains strings from both lists in sequence.
  4. We print the combined iterable to standard output.

Dart Program

void main() {
  var words1 = ['apple', 'banana'];
  var words2 = ['cherry', 'date'];
  var combined = words1.followedBy(words2);
  print(combined); // Output: (apple, banana, cherry, date)
}

Output

(apple, banana, cherry, date)

Summary

In this Dart tutorial, we learned about followedBy() method of List: the syntax and few working examples with output and detailed explanation for each example.