Trying to fill in a List of class

by Mirko Urania   Last Updated May 22, 2020 23:26 PM

I'm really sorry I'm such a beginner... At the end I just have a list of 8 TimeCardDayStrip with the same name/position when it'd supposed to be the 8 different names/roles. I don't understand what I'm doing wrong here: thank you very much

'''
class TimeCardDayStrip {
  String name, position;
  DateTime day;

  @override
  String toString(){
    return '{ $this.name, $this.position }';
  }
}

void main() {
  var tcds = TimeCardDayStrip();
  var listOfTcds = [];
  List<String> names, roles;
   names = ["Michael", "Gunnell", "Byrne", "Aspromonte", "Davis", "Adam Jordan", "Mirko"];
  roles = ["Director", "Vice", "President", "1ST", "KEY 2ND", "2ND", "BASECAMP PA", "PA", " PA", "Add'l PA"];

  for (int i = 0; i < names.length; i++) {
    tcds.name = names[i];
    tcds.position = roles[i];
    listOfTcds.add(tcds);
    // checking the list as it creates
    print(listOfTcds[i].name+' is '+listOfTcds[i].position);
  }
  //print the list of card to check
  print('print the list of time card to check');
  for (int x = 0; x < listOfTcds.length; x++){
    print (listOfTcds.elementAt(x).name);
  }
}
'''


Answers 2


If you just want to print out each name:

 listOfTcds[x].name 
Jotcode
Jotcode
May 22, 2020 23:22 PM

I believe you need to declare the tcds as Tcds = new TimecardDayStrip() and not just tcds = TimeCardDayStrip() or you are just changing the same object and not creating a new instance.

tyler powell
tyler powell
May 22, 2020 23:25 PM

Related Questions


How to implement selectable DataRow in flutter

Updated January 13, 2019 20:26 PM



How to render list inside list in flutter?

Updated May 20, 2019 10:26 AM