Merging 2 lists in Dart

July 2, 2020 Linda Walker Loading... Post a comment

The List class of Dart provides the addAll method that helps you easily join 2 lists together;

Example:

void main() {
  List listA = [1, 2, 3];
  List listB = [4, 5, 6];
  listA.addAll(listB);
  print(listA);
  
  List<String> listC = ['Dog', 'Cat'];
  List<String> listD = ['Bear', 'Tiger'];
  listC.addAll(listD);
  print(listC);
}

Output:

[1, 2, 3, 4, 5, 6]
[Dog, Cat, Bear, Tiger]
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles

0
Would love your thoughts, please comment.x
()
x