Thuta Learning
IntermediateProgrammingbeginner

Classes & Objects

Relax. We'll talk through this in plain words — no textbook voice.

Class is the blueprint for an object. It stores data as fields and behavior as methods. In app development, you'll typically model real-world entities like User, Product, Order, and Course as classes.

dart
class Course {
  String title;
  int lessons;

  Course(this.title, this.lessons);

  void describe() {
    print('$title has $lessons lessons.');
  }
}

void main() {
  final dartCourse = Course('Dart Basic', 18);
  dartCourse.describe();
}

Course class has title and lessons fields. The constructor supplies values when the object is created. describe() method uses the object's data to produce a message.

You should see
Dart Basic has 18 lessons.

Easy traps

  • Declare a field in a class but forget to give it a value in the constructor, and you'll get a non-nullable field error. That's Dart's null safety making sure you never end up with an object missing data.