by @kodeazy

What are the different ways to combine two Lists in flutter?

Home » flutter » What are the different ways to combine two Lists in flutter?

In this blog we will discuss on different ways to combine Lists in flutter
Below are the different ways to combine.

Using addAll() function

void main() {
 List l1=[1,2,3];
  List l2=[4,5,6];
  List l3=l1..addAll(l2);
  print(l3);
}

Output

[1, 2, 3, 4, 5, 6]

To combine multiple lists we have expand() function

void main() {
 List l1=[1,2,3];
  List l2=[4,5,6];
  List l3=[7,8,9];
  List l4=[l1,l2,l3].expand((x) => x).toList();
  print(l4);
}

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Using Spread Operator

void main() {
 List l1=[1,2,3];
  List l2=[4,5,6];
  List l3=[7,8,9];
  List l4=[...l1,...l2,...l3];
  print(l4);
}

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Using + Operator

void main() {
 List l1=[1,2,3];
  List l2=[4,5,6];
  List l3=[7,8,9];
  List l4=l1+l2+l3;
  print(l4);
}

output

[1, 2, 3, 4, 5, 6, 7, 8, 9]