How to Combine Two or More List Array in Dart/Flutter

In Dart, Array is called List. In this example, we are going to show you different ways to concat or combine two or more list array in Dart. You can apply this method in Flutter framework to add list. See the example below:

void main(){
    //some sample arrays
    List<String> asia = ["Nepal", "India", "Pakistan", "China", "Japan"];
    List<String> europe = ["Germany", "France", "United Kingdom", "Spain"];
    List<String> africa = ["Kenya", "Nigeria", "Ethiopia", "Egypt"];

   var list = new List.from(asia)..addAll(europe);
   print(list);
   //Output: [Nepal, India, Pakistan, China, Japan, Germany, France, United Kingdom, Spain]

   var list1 = new List.from(asia)..addAll(europe)..addAll(africa);
   print(list1);
   //output: [Nepal, India, Pakistan, China, Japan, Germany, France, United Kingdom, Spain, Kenya, Nigeria, Ethiopia, Egypt]

   var list2 = asia + africa + europe;
   print(list2);
   //Output: [Nepal, India, Pakistan, China, Japan, Kenya, Nigeria, Ethiopia, Egypt, Germany, France, United Kingdom, Spain]
   
   var list3 = [...europe, ...africa, ...asia];
   print(list3);
   //Output: [Germany, France, United Kingdom, Spain, Kenya, Nigeria, Ethiopia, Egypt, Nepal, India, Pakistan, China, Japan]

   var list4 = [africa, europe].expand((x) => x).toList();
   print(list4);
   //Output: [Kenya, Nigeria, Ethiopia, Egypt, Germany, France, United Kingdom, Spain] 
}

You can combile Object/modal list as will using above methods.

void main(){
  
  //list of objects/modal class
   List<User> class1 = [
       User("Anil Chaudhary", "Nepal"),
       User("Max John", "America"),
       User("Uttam Shing", "India"),
       User("Imran Khan", "Pakistan")
   ]; 

   List<User> class2 = [
       User("Bihsal Karki", "Nepal"),
       User("Harry Potter", "America"),
   ];

   var userlist1 = new List.from(class1)..addAll(class2);

  //you can do with different methods mentioned in plain format example.
   
}


class User{
   String name, address;
   User(this.name, this.address);
}

You can add these modal list using different methods explained in top example.

In this way, you can add two or more List array in Dart/Flutter.

No any Comments on this Article


Please Wait...