Hello I am wondering if it is possible to make an array of objects in dart/flutter.
class Student {
int math;
}
Student[] studentArray = new
Student[7];
studentArray[0] = new Student();
void main() {
List persons = [User("foo"), User("bar")]; //non-empty on create
List users = []; //blank initially
for (int i = 0; i < 5; i++) {
users.add("User $i");
}
//print using iterator
for (var u in users) {
print(u);
}
/*
* first element can be accessed using users[0] or users.first
* last element can be accessed using users.last
* */
}
class User {
String name;
User(this.name);
}
To make array of objects in dart we have List. In your scenario to get array of Student type objects we define List of type student and add the objects of student inside the List. Below is the sample example code of adding Student objects into a List.
List<Student > StudentDetails = [new Student (),new Student ()];
for more information about how to add objects into list refer to the link https://kodeazy.com/flutter-array-userdefined-class-objects/
Listclass - the docs say: "An indexable collection of objects with a length."Listcreated with the constructor that takes a length parameter:List(len). This is not the same as[], the former is non-growable, the latter can grow.