Home > Net >  List<StudentModel> studentDetails = []; here StudentModel is class in flutter and i don't
List<StudentModel> studentDetails = []; here StudentModel is class in flutter and i don't

Time:01-05

class StudentModel {
final String name;
final String age;

StudentModel({required this.name, required this.age});
}

this is StudentModel class

I didn't understand how the values will store in this List

CodePudding user response:

You can follow the below code to add elements to your list.

 List<StudentModel> studentDetails = [];

 StudentModel firstStudent = StudentModel(name: "John", age: "16");
 StudentModel secondStudent = StudentModel(name: "Doe", age: "17");

 studentDetails.add(firstStudent);
 studentDetails.add(secondStudent);

 studentDetails.forEach((student) {
   print(student.name);
 });

// Prints:
// John
// Doe

studentDetails is basically a list that contains n number of StudentModel objects. You can visualize it being the following form:

studentDetails = [
    StudentModel(name: "John", age: "16"),
    StudentModel(name: "Doe", age: "17"),
  ];

CodePudding user response:

You can use below code snippet for understanding

// Create object to add students
List<StudentModel> students = [];

// Adding student details to list
students.add(StudentModel(name: "Amitabh", age: "65"));
students.add(StudentModel(name: "Akshay", age: "72"));

// Iterate list to see output
students.forEach((student) {
   print(student.name);
   print(student.age);
});

// Output:
// Amitabh
// 65
// Akshay
// 72

Here we created a object names students for storing a list of students in next line we are adding student details to list with name and age keys (both keys are required as mention in model) then we iterate the student list to print student details which we have added.

CodePudding user response:

If you are talking about how this class constructor works then here is some explanation.

In flutter, when you are creating this type of class there are few ways to set value which depends how you created the constructor of data model class.

1. Named parameters

class StudentModel {
   final String name;
   final String age;

   StudentModel({required this.name, required this.age});
}

This type of parameters inside {} is called named parameters. To set these kind of values you need to specify the name of the parameter in which you want to set value like this.

StudentModel _studentModel = StudentModel(name: "John", age: "30");

This will also allows you to setup the required parameters. If you don't want to use the name each time & simply want to set values then you can second approach.

2. Unnamed parameters

class StudentModel {
   final String name;
   final String age;

   StudentModel(this.name, this.age});
}

Here you can just remove the curly braces. Which allows you to set values like this.

StudentModel _studentModel = StudentModel("John", "30");

3. Optional parameters In both of above types you have to pass all the parameters because they are required as mentioned in 1st approach & second approach make them required by default. But if you want to set some parameters as optional in both ways you can do this.

In named you can write like this

StudentModel({required this.name, this.age = 20}); // age must be specified

In unnamed you can try this

StudentModel(this.name, [this.age = 20]); // age must be specified within []

Hope this will help

  • Related