Hello friends, We’re back with a simple but useful tutorial of Dart. In today’s tutorial we would learn about add data from one list to another list in flutter dart. It’s like cloning a List into another List. But there is a catch The List we would copy from and the List we copy to will be same type then it will works. If any of List type is different then it will not work. We would use List.addAll(List Name) inbuilt function of dart here to perform this functionality. So in this tutorial we would learn about Example of Combine Copy One List to Another in Flutter Dart.
Contents in this project Example of Combine Copy One List to Another in Flutter Dart :-
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Creating our void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main class MyApp extends StatelessWidget.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating 2 Types of List Array, First one is a String type of List, Second is Integer type of List. We would create 4 List here.
1 2 3 4 5 6 7 |
List<String> alphabet_1 = <String>['A', 'B', 'C']; List<String> alphabet_2 = <String>['D', 'E', 'F']; List listONE = [1, 2, 3]; List listTWO = [4, 5, 6]; |
5. Creating a function named as copyList(). In this function we would simply copy one list to another using ListName.addAll(List Name) function. Then we would print the result into console window using print method.
1 2 3 4 5 6 7 8 9 |
void copyList() { alphabet_1.addAll(alphabet_2); listONE.addAll(listTWO); print(alphabet_1); print(listONE); } |
6. Creating Widget Build area then we would make a Button and call copyList() function on button onPressed event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Combine Copy One List to Another in Dart')), 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('Copy One List to Another in Dart'), onPressed: copyList, ), ), ), ))); } |
7. 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 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { List<String> alphabet_1 = <String>['A', 'B', 'C']; List<String> alphabet_2 = <String>['D', 'E', 'F']; List listONE = [1, 2, 3]; List listTWO = [4, 5, 6]; void copyList() { alphabet_1.addAll(alphabet_2); listONE.addAll(listTWO); print(alphabet_1); print(listONE); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Combine Copy One List to Another in Dart')), 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('Copy One List to Another in Dart'), onPressed: copyList, ), ), ), ))); } } |
Screenshots :-