Classes & Objects
A class describes fields and methods; ClassName(...)
creates an object from it, Dart doesn't require the new
keyword, unlike Java or C#.
Example: a Student class
class Student {
String name;
String track;
Student(this.name, this.track);
}
void main() {
var ada = Student('Ada', 'Frontend');
print('${ada.name} is studying ${ada.track}');
}
Ada is studying Frontend
Student(this.name, this.track); is Dart's compact
constructor shorthand, this.name directly assigns the
constructor's first argument to the object's own name field,
with no separate assignment line needed inside the constructor body, the
way Java or C# would require. This exact same "class describes a blueprint,
new instances get created from it" pattern is precisely how Flutter builds
every single UI widget, which is exactly why learning Dart's classes
first makes Flutter's structure click much faster.
class Student {
String name;
String track;
Student(this.name, this.track);
}
void main() {
var ada = Student('Ada', 'Frontend');
print('${ada.name} is studying ${ada.track}');
}
Ada is studying Frontend