We all know about List in dart. List is used to store same or different type of data into a single Object. Where we can access the data using its index number. Sometimes we need to Join two list together. So in today’s tutorial we would learn about Example of Join Merge Combine Two List in Flutter Dart. There are 3 different ways to Merge two list together. We would discuss all of them one by one. The first and most common method is using addAll() function. Second method is using forEach() loop. The third and final method is using Spread Operator known as Tree Dot operator. So let’s get started 🙂 .
Contents in this project Join Merge Combine Two List in Flutter Dart :-
1. Combine List using addAll :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void main(){ List<String> stringList1 = <String>['One', 'Two', 'Three']; List<String> stringList2 = <String>['A', 'B', 'C']; List numberList1 = [10, 9, 8]; List numberList2 = [7, 6, 5]; //Join Same Type of List Using addAll() stringList1.addAll(stringList2); //Join Same Type of List Using addAll() numberList1.addAll(numberList2); print(stringList1); print(numberList1); } |
Output :
1 2 |
[One, Two, Three, A, B, C] [10, 9, 8, 7, 6, 5] |
2. Join List using forEach Loop :-
1 2 3 4 5 6 7 8 9 10 11 |
void main(){ List stringList3 = ['Pankaj', 'Manish', 'Juned']; List numberList3 = [100, 99, 'One', 98]; //Join Different Type of List Using forEach() numberList3.forEach((element) => stringList3.add(element)); print(stringList3); } |
Output :
1 |
[Pankaj, Manish, Juned, 100, 99, One, 98] |
3. Merge List using Spread Operator :-
1 2 3 4 5 6 7 8 9 10 11 |
void main(){ List<String> stringList2 = <String>['A', 'B', 'C']; List numberList1 = [10, 9, 8]; //Join Different Type of List Using Spread Operator. List newList = [...numberList1, ...stringList2]; print(newList); } |
Output :
1 |
[10, 9, 8, A, B, C] |
4. Complete source code for main.dart file :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { List<String> stringList1 = <String>['One', 'Two', 'Three']; List<String> stringList2 = <String>['A', 'B', 'C']; List stringList3 = ['Pankaj', 'Manish', 'Juned']; List numberList1 = [10, 9, 8]; List numberList2 = [7, 6, 5]; List numberList3 = [100, 99, 'One', 98]; void copyList() { //Join Same Type of List Using addAll() stringList1.addAll(stringList2); //Join Same Type of List Using addAll() numberList1.addAll(numberList2); //Join Different Type of List Using forEach() numberList3.forEach((element) => stringList3.add(element)); //Join Different Type of List Using Spread Operator. List newList = [...numberList1, ...stringList2]; print(stringList1); print(numberList1); print(stringList3); print(newList); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Center( child: Container( margin: const EdgeInsets.all(8), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.lightGreen[900], padding: const EdgeInsets.all(12), textStyle: const TextStyle(fontSize: 20), ), child: const Text('Join Copy List in Dart'), onPressed: copyList, ), ), ), ))); } } |
Screenshot of App :