Wednesday, February 6, 2013

Setter and Getter for private property, in dart.

To define private member in dart, prefix the name with underscore(_) character. This example demonstrate how to implement setter and getter for private member.

class Visitor{
  var _name;   //private property
  
  set name(value) => _name = value; //setter for private property
  get name => _name;                //getter for private property
}
void main() {
  var visitor = new Visitor();
  visitor.name = "Erik";
  print ("Hello, ${visitor.name}");
}


Setter and Getter for private property
Setter and Getter for private property

No comments:

Post a Comment