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
Parameter | Optional/Required | Description |
---|---|---|
other | required | the 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,
- We create two lists,
numbers1
andnumbers2
, containing integers. - We then use the
followedBy()
method onnumbers1
to concatenate it withnumbers2
. - The resulting iterable
combined
contains elements from both lists in sequence. - 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,
- We create two lists,
letters1
andletters2
, containing characters. - We then use the
followedBy()
method onletters1
to concatenate it withletters2
. - The resulting iterable
combined
contains characters from both lists in sequence. - 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,
- We create two lists,
words1
andwords2
, containing strings. - We then use the
followedBy()
method onwords1
to concatenate it withwords2
. - The resulting iterable
combined
contains strings from both lists in sequence. - 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.