Dart – Join Two Lists
You can join two lists in Dart by appending one list to another or by creating a new list that contains the elements of both lists. The appropriate method depends on whether the original list should be modified.
Use addAll() when you want to modify an existing growable list. Use the spread operator, followedBy(), or List.of() when you want to keep the original lists unchanged.
Join Dart Lists Using List.addAll()
In this example, we take two lists list1 and list2. We then use addAll() to append every element of list2 to list1.
List.addAll() modifies the list on which it is called. Therefore, list1 changes, while list2 remains unchanged.
Dart Program
void main(){
List list1 = [24, 'Hello', 84];
List list2 = [41, 65];
//join list2 to list1
list1.addAll(list2);
print(list1);
}
Output
[24, Hello, 84, 41, 65]
The elements are appended in their existing order. If either list is empty, the operation still works: adding an empty list makes no change, while adding a non-empty list to an empty growable list copies all its elements.
Create a New Combined Dart List with the Spread Operator
The spread operator ... is a concise way to create a new list from two or more existing lists. It does not modify the source lists.
void main() {
final list1 = [10, 20, 30];
final list2 = [40, 50];
final joinedList = [...list1, ...list2];
print(joinedList);
print(list1);
print(list2);
}
Output
[10, 20, 30, 40, 50]
[10, 20, 30]
[40, 50]
This approach is generally suitable when the result should be stored separately from both input lists.
Join Nullable Dart Lists with the Null-Aware Spread Operator
When a source list may be null, use the null-aware spread operator ...?. A null list contributes no elements to the result.
void main() {
List<int>? first = [1, 2];
List<int>? second;
final joinedList = [...?first, ...?second, 3];
print(joinedList);
}
Output
[1, 2, 3]
Join Dart Lists Using followedBy()
The followedBy() method combines two iterables without immediately creating a list. It returns an Iterable, so call toList() when a List result is required.
void main() {
final first = ['red', 'green'];
final second = ['blue', 'black'];
final joinedList = first.followedBy(second).toList();
print(joinedList);
}
Output
[red, green, blue, black]
This method is useful when code already works with Iterable values or when additional iterable operations, such as where() or map(), will be applied before creating the final list.
Copy the First List Before Appending the Second List
You can also copy the first list and then append the second list. This keeps the source lists unchanged while allowing the result to be built with addAll().
void main() {
final list1 = [5, 10];
final list2 = [15, 20];
final joinedList = List<int>.of(list1)..addAll(list2);
print(joinedList);
print(list1);
}
Output
[5, 10, 15, 20]
[5, 10]
Add Elements of Another Dart List One by One
You can traverse the second list and add each element to the first list. This produces the same result as addAll(), but it is more verbose when no per-element processing is required.
Dart Program
void main(){
List list1 = [24, 'Hello', 84];
List list2 = [41, 65];
//join list2 to list1
list2.forEach((element) => list1.add(element));
print(list1);
}
Output
[24, Hello, 84, 41, 65]
An explicit loop is useful when each element needs to be checked, transformed, or filtered before it is added.
void main() {
final first = [2, 4];
final second = [3, 6, 9, 12];
for (final number in second) {
if (number.isEven) {
first.add(number);
}
}
print(first);
}
Output
[2, 4, 6, 12]
Join More Than Two Lists in Dart
The spread operator can combine any number of lists in a single list literal.
void main() {
final first = [1, 2];
final second = [3, 4];
final third = [5, 6];
final joinedList = [...first, ...second, ...third];
print(joinedList);
}
Output
[1, 2, 3, 4, 5, 6]
Join Dart Lists Without Duplicate Values
Joining lists does not remove duplicates automatically. To keep only unique values, combine the lists and convert the result to a Set before converting it back to a list.
void main() {
final first = [1, 2, 3];
final second = [3, 4, 5];
final joinedList = {...first, ...second}.toList();
print(joinedList);
}
Output
[1, 2, 3, 4, 5]
A Dart Set keeps one occurrence of each equal value. Use this only when duplicate removal is part of the requirement.
Joining Fixed-Length and Unmodifiable Dart Lists
addAll() requires a growable, modifiable list. It cannot expand a fixed-length list, and it cannot modify an unmodifiable list. In these cases, create a new list with the spread operator or List.of().
void main() {
final fixedList = List<int>.filled(2, 0, growable: false);
final otherList = [1, 2];
final joinedList = [...fixedList, ...otherList];
print(joinedList);
}
Output
[0, 0, 1, 2]
Choosing a Dart List Joining Method
| Requirement | Suitable method | Result |
|---|---|---|
| Modify the first growable list | first.addAll(second) | The first list is changed |
| Create a separate combined list | [...first, ...second] | A new list is created |
| Combine nullable lists | [...?first, ...?second] | Null lists are skipped |
| Continue working as an iterable | first.followedBy(second) | An Iterable is returned |
| Filter or transform while joining | A for loop | Elements can be processed individually |
| Remove duplicate values | {...first, ...second}.toList() | A list of unique values is created |
Common Questions About Joining Lists in Dart
Does addAll() create a new list in Dart?
No. addAll() appends elements to the existing list on which it is called. Use a spread list such as [...first, ...second] when a new list is required.
Does joining two Dart lists remove duplicates?
No. Methods such as addAll(), the spread operator, and followedBy() preserve duplicate elements. Convert the combined values to a Set when duplicates must be removed.
How can I join two Dart lists without changing either list?
Create a new list with [...first, ...second], use first.followedBy(second).toList(), or copy one list before calling addAll().
Can Dart join lists containing different value types?
Yes, when the destination type permits all the values. For type-safe code, use a shared element type such as List<num> for integers and doubles or List<Object> for unrelated object types.
Summary of Dart List Joining Methods
Use addAll() to append one list to a growable list in place. Use the spread operator to create a new combined list, ...? for nullable lists, and followedBy() when an iterable result is useful. In this Dart Tutorial, we learned how to join two or more lists while controlling mutation, duplicates, null values, and the result type.
TutorialKart.com