by @kodeazy
flutter how to create array of user defined objects in a list?
- In this blog we will discuss on how to create array of user defined objects using
list
in flutter. -
Below is the syntax of creating userdefined
list
.List<SomeNullableType> eXampleList = []
- Here in above syntax in place of
SomeNullableType
we specify the type ofObjects
to store the data. -
Below is an example syntax to store objects of type
Employee
in a list.List<Empoyee> EmployeeDetails = [ new Empoyee(27, 103, "Suresh") ];
- In the above syntax we created a list and added objects of type
Employee
into the list. - Below is the sample program of creating
array
ofEmployee
objects.
void main() {
List<Empoyee> EmployeeDetails = [
new Empoyee(27, 103, "Suresh"),
new Empoyee(23, 102, "Ramesh"),
new Empoyee(21, 105, "Mahesh"),
new Empoyee(29, 104, "Gopi")
];
EmployeeDetails.forEach((emp) {
print("Name is :" +
emp.name +
" Age is:" +
(emp.age).toString() +
" Salary is :" +
(emp.age).toString());
});
}
class Empoyee {
int age;
int salary;
String name;
Empoyee(@required this.age, @required this.salary, @required this.name) {}
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.yellow,
body: Center(child: new Text("Hello, World!"))),
);
}
}
Output:
Name is :Suresh Age is:27 Salary is :27
Name is :Ramesh Age is:23 Salary is :23
Name is :Mahesh Age is:21 Salary is :21
Name is :Gopi Age is:29 Salary is :29