skip to Main Content
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

4

Answers


  1. 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"),
      ];
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. List studentDetails = [];

    The above code is used to initialize "empty" studentDetails list. Also, by declaring this every time you load this widget the list will be initialize as empty.

    By add function you can add values to list

    Ex. studentDetails.add(StudentModel(name: "Rishabh Gupta", age: "23")));

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search