How to Combine Lists in Dart with Example
A List in Dart Programming is very similar to arrays in other programming languages. It is an Ordered collection of Objects wherein the order matters. In Dart, you can combine or merge two or more lists using various methods.
The two most common methods are to use the addAll
method or the +
operator but the List can also be combined using other methods such as theexpand()
method of the list or using spread
the operator. I will be explaining all the examples with explanation below.
You can Combine two existing lists and create a new resulting list or you can also Append one list to another list this is possible as Lists are Mutable meaning you can add, remove, or reorder the elements in a List. So, you can add elements of another list into one another.
Do check out the Complete Dart Flutter Course. This Course is A Complete Guide / Tutorial to the Flutter & Dart SDK for building native Android, iOS, Web, and desktop Applications!
1. Combining List using the addAll() Method
In the below example, we will be creating a new list by adding elements of Lists to the third list. but you can avoid creating a new list and append one list to another.
void main() {
// Create two lists to combine
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
// Combine the lists using the 'addAll' method
List<int> combinedList = [];
combinedList.addAll(list1);
combinedList.addAll(list2);
// Print the combined lists
print("Combined List (Using 'addAll' method): $combinedList");
}
void main() {
// Create two lists to combine
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
// Combine the lists using the 'addAll' method
list1.addAll(list2);
// Print the combined lists
print("Combined List (Using 'addAll' method): $list1");
}
list1
& list2
are declared and initialized with integer elements. then we created an empty list combinedList
which we will use to store the combined results. Now we have added list1 and list2 elements to the combined list using combinedList.addAll(list1)
& combinedList.addAll(list12).
2. Combining List using ‘+’ Operator
void main() {
// Create two lists to combine
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
// Alternatively, you can use the '+' operator to combine lists
List<int> combinedListUsingOperator = list1 + list2;
// Print the combined lists
print("Combined List (Using '+' operator): $combinedListUsingOperator");
}
the Concatenation Operator ‘+” can be used to combine or add two Lists. you can see in the above code we have combined two lists and saved them to another list named combinedListUsingOperator List<int> combinedListUsingOperator = list1 + list2.
3. Combining List using Expand() Method
The expand()
method is used to flatten and transform a list of lists into a single list. Here's an example:
void main() {
List<List<int>> listOfLists = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Combine lists using the 'expand()' method
List<int> combinedList = listOfLists.expand((list) => list).toList();
// Print the combined list
print("Combined List (Using 'expand()' method): $combinedList");
}
- We define a list of lists,
listOfLists
, where each inner list contains integers. - We use the
expand()
method to combine these lists into a single list,combinedList
. Theexpand()
method takes a function as an argument, which is applied to each element of the outer list. In this case, the function takes an inner list and returns it as-is, effectively flattening the list of lists. - We convert the result to a regular list using
toList()
. - Finally, we print the combined list, which contains all the elements from the inner lists combined into one.
4. Combining List using Spread('…’) Operator
The spread (...
) operator allows you to insert all elements from one list into another list. Here's an example:
void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
// Combine lists using the spread operator
List<int> combinedList = [...list1, ...list2];
// Print the combined list
print("Combined List (Using spread operator): $combinedList");
}
We use the spread (...
) operator to combine the elements of list1
and list2
into a new list, combinedList
. The ...
the operator inserts all elements from list1
followed by all elements from list2
into combinedList
.
Both the expand()
method and the spread (...
) operators are useful for combining lists in Dart, and you can choose the one that best suits your specific use case. The expand()
method is especially handy when you have a list of lists, while the spread operator is more concise for combining regular lists.
Also Read: