Tsallaka zuwa abun ciki
Justice Tech School

Dart Classes and Objects

A class has fields and methods. Create objects by calling the constructor, and new is optional. Shorthand constructors like this.name set fields directly.

Example
class Student {
  String name;
  int score;
  Student(this.name, [this.score = 0]);
  String describe() => "$name: $score";
}
void main() {
  var s = Student("Ada", 70);
  print(s.describe());
}

Exercise

Set a field in the constructor.

Student(.name);

Quiz Test

Is new required?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.