To join lists in Dart, you can use the `addAll` method to combine multiple lists into a single list.
In this example,
list1
and list2
with integer values.joinedList
and use the addAll
method to add all elements from list1
and list2
to joinedList
.void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
List<int> joinedList = [];
joinedList.addAll(list1);
joinedList.addAll(list2);
print('Joined List: \$joinedList');
}
Joined List: [1, 2, 3, 4, 5, 6]
In this example,
listA
, listB
, and listC
with string values.resultList
and use the addAll
method to add all elements from listA
, listB
, and listC
to resultList
.void main() {
List<String> listA = ['apple', 'banana'];
List<String> listB = ['cherry', 'date'];
List<String> listC = ['fig', 'grape'];
List<String> resultList = [];
resultList.addAll(listA);
resultList.addAll(listB);
resultList.addAll(listC);
print('Joined List: \$resultList');
}
Joined List: [apple, banana, cherry, date, fig, grape]
In this tutorial, we learned How to Join Lists in Dart language with well detailed examples.