In Flutter copying list into another list is used in many dynamic applications where app developer dose not wants to change the original List. The List library gives us a method named as List.from(old_List). Using this method we can easily make clone of Old List. This method is works on all type of List whether it is String or Integer. So in this tutorial we would Flutter Dart Copy Clone List into Another List on Button Click Android iOS Example.
Contents in this project Flutter Dart Copy Clone List into Another List Android iOS Example:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp extends StatelessWidget. This is our Root Parent View class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating a Integer List named as intList with some integer value. Here we are creating List with official List declaration syntax.
1 |
List<int> intList = [1, 2, 3, 4, 5, 6, 7, 8]; |
5. Creating another List named as stringList with few String values. Here we are creating List with var variable syntax.
1 |
var stringList = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; |
6. Creating a function named as copyList(). In this function we would copy both above List into new List using List.from() function. After copying the List we would print the copying list into Terminal using print().
1 2 3 4 5 6 7 8 9 10 11 |
void copyList() { List<int> newList_1 = List.from(intList); var newList_2 = List.from(stringList); print(newList_1); print(newList_2); } |
7. Creating Widget Build Area in MyApp class. Now we would make a Raised Button and call the copyList() function.
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 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => copyList(), child: Text(' Click Here To Copy List into Another List'), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
8. 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 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { List<int> intList = [1, 2, 3, 4, 5, 6, 7, 8]; var stringList = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; void copyList() { List<int> newList_1 = List.from(intList); var newList_2 = List.from(stringList); print(newList_1); print(newList_2); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => copyList(), child: Text(' Click Here To Copy List into Another List'), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshot:
