To concatenate two lists in Dart, you can use the spread operator (...
) or the addAll
method.
In this example,
list1
and list2
with initial values....
) to concatenate list2
to list1
.void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
List<int> concatenatedList = [...list1, ...list2];
print('Concatenated list: \$concatenatedList');
}
Concatenated list: [1, 2, 3, 4, 5, 6]
In this example,
list1
and list2
with initial values.addAll
method to append all elements of list2
to list1
.void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
list1.addAll(list2);
print('Concatenated list: \$list1');
}
Concatenated list: [1, 2, 3, 4, 5, 6]
In this tutorial, we learned How to Concatenate Two Lists in Dart language with well detailed examples.