How to Combine Lists in Dart?

In Dart programming, the List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects. The core libraries in Dart are responsible for the existence of List class, its creation, and manipulation. There are 5 ways to combine two or more list:

  1. Using addAll() method to add all the elements of another list to the existing list.
  2. Creating a new list by adding two or more lists using addAll() method of the list.
  3. Creating a new list by adding two or more list using expand() method of the list.
  4. Using + operator to combine list.
  5. Using spread operator to combine list.

Using addAll() method to add all the elements of other lists to the existing list

We can add all the elements of the other list to the existing list by the use of addAll() method. To learn about this method you can follow this article.

Example:
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
main() {
    
  // Creating lists
  List gfg1 = ['Welcome','to'];
  List gfg2 = ['GeeksForGeeks'];
    
  // Combining lists
  gfg1.addAll(gfg2);
    
  // Printing combined list
  print(gfg1);
}

chevron_right


 
Output: 



[Welcome, to, GeeksForGeeks]

Creating a new list by adding two or more list using addAll() method of list

We can add all the elements of the list one after another to a new list by the use of addAll() method in Dart. To learn about this method you can follow this article.

Example:
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
main() {
    
  // Creating lists
  List gfg1 = ['Welcome','to'];
  List gfg2 = ['GeeksForGeeks'];
    
  // Combining lists
  var newgfgList = new List.from(gfg1)..addAll(gfg2);
    
  // Printing combined list
  print(newgfgList);
}

chevron_right


 
Output: 

[Welcome, to, GeeksForGeeks]

Creating a new list by adding two or more list using expand() method of list

We can add all the elements of the list one after another to a new list by the use of expand() method in Dart. This is generally used to add more than two lists together.

Example:
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
main() {
    
  // Creating lists
  List gfg1 = ['Welcome'];
  List gfg2 = ['to'];
  List gfg3 = ['GeeksForGeeks'];
    
  // Combining lists
  var newgfgList = [gfg1, gfg2, gfg3].expand((x) => x).toList();
    
  // Printing combined list
  print(newgfgList);
}

chevron_right


 
Output: 



[Welcome, to, GeeksForGeeks]

Using + operator to combine list

We can also add lists together by the use of + operator in Dart. This method was introduced in the Dart 2.0 update.

Example:
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
main() {
    
  // Creating lists
  List gfg1 = ['Welcome'];
  List gfg2 = ['to'];
  List gfg3 = ['GeeksForGeeks'];
    
  // Combining lists
  var newgfgList = gfg1 + gfg2 + gfg3;
    
  // Printing combined list
  print(newgfgList);
}

chevron_right


 
Output: 

[Welcome, to, GeeksForGeeks]

Using spread operator to combine the list

As of Dart 2.3 update, one can also use the spread operator to combine the list in Dart.

Example:
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
main() {
    
  // Creating lists
  List gfg1 = ['Welcome'];
  List gfg2 = ['to'];
  List gfg3 = ['GeeksForGeeks'];
    
  // Combining lists
  var newgfgList = [...gfg1, ...gfg2, ...gfg3];
    
  // Printing combined list
  print(newgfgList);
}

chevron_right


 
Output: 

[Welcome, to, GeeksForGeeks]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.