Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Class Objects operation in Dart

Tags:

dart

Have an issue in the below chunk of code.

class Events
{
 // some member variables
}


class SVList
{ 
  String name;
  int contentLen;
  List<Events> listEvents;  

  SVList()
  {
    this.name = "";
    this.contentLen = 0;
    this.listEvents = new List<Events>();
  }
}

class GList
{
  List<SVList> listSVList;

  GList(int Num)
  {
   this.listSVList = new List<SvList>(num);   
  } 
}


 function f1 ()
 {
   //array of class objects 
   GList gList = new GList(num);
 }

Not able to find "listEvents" member after GList constructor is called. Am I missing anything here.

Referencing glist.listSVList[index] --> do not find member variable 'listEvents'. Any pointers appreciated.

To elaborate , no member variable with 'glist.listSVList[index].listEvents' is found.

like image 317
Sudhi Avatar asked Dec 12 '25 17:12

Sudhi


1 Answers

you have a typo here:

this.listSVList = new List<SvList>(num);  // <== SVList not SvList

function seems wrong here

function f1 () { ... }

in this case you use function as a return type

another typo:

GList(int Num) // <== Num (uppercase)
{
  this.listSVList = new List<SvList>(num);   // <== num (lowercase)
} 

this code worked:

class Events {
  // some member variables
}

class SVList {
  String name;
  int contentLen;
  List<Events> listEvents;

  SVList() {
    this.name = "";
    this.contentLen = 0;
    this.listEvents = new List<Events>();
  }
}

class GList {
  List<SVList> listSVList;

  GList(int num) {
    this.listSVList = new List<SVList>(num);
  }
}


main() {
  //array of class objects
  GList gList = new GList(5);
  gList.listSVList[0] = new SVList();
  gList.listSVList[0].listEvents.add(new Events());
  print(gList.listSVList[0].listEvents.length);
}

What editor are you using?
DartEditor showed all errors immediately after I pasted your code.

like image 187
Günter Zöchbauer Avatar answered Dec 16 '25 11:12

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!