6

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();

2
  • 1
    see List class - the docs say: "An indexable collection of objects with a length." Commented Nov 29, 2019 at 17:16
  • The equivalent of an array in Dart is a List created 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. Commented Nov 29, 2019 at 20:57

3 Answers 3

4
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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank, I will try this out
1

Sure thing

import 'dart:collection';

class MyClass {
    List<MyObjectClass> myList = [];
}

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.