Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

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:

How to Combile Tow or More Plain List Array in Dart/Flutter:

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.

How to Combile Object List array in Dart/Flutter:

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...