A class groups data and behavior. Rectangle(this.width, this.height) is the constructor shorthand.

Program

Play the program to build a rectangle and call its area method.

classes.dart
Replay: real traced execution (multi-file project)
class Rectangle {
  final int width;
  final int height;
  Rectangle(this.width, this.height);
  int area() => width * height;
}

void main() {
  var rect = Rectangle(4, 3);
  print(rect.area());
}
  1. rect ← Rectangle(width: 4, height: 3)

    8void main() {9  var rect = Rectangle(4, 3);10  print(rect.area());
    values this stepRectangle(width: 4, height: 3)rect
  2. call ← rect.area()

    9  var rect = Rectangle(4, 3);10  print(rect.area());11}
    values this steprect.area()call4 x 3rect
  3. return value ← 12

    4  Rectangle(this.width, this.height);5  int area() => width * height;6}
    values this step12return value4this.width3this.height
  4. print(rect.area());

    9  var rect = Rectangle(4, 3);10  print(rect.area());11}
    output12
class `class Rectangle` groups fields and methods.
this.x constructor `Rectangle(this.width, this.height)` assigns straight to fields.
arrow method `=>` is a one-expression method body.