NANDHOO.

Dart Object-Oriented Programming

Dart Object-Oriented Programming

Dart is a true object-oriented language. Classes and objects are at the heart of everything you build in Flutter. In fact, every Widget you'll use is a class. Understanding how to structure classes, use constructors, and leverage inheritance is vital for building scalable apps.

Why This Topic Matters

In Flutter, "Everything is a Widget," and "Every Widget is a Class." When you want to create a custom button, a specialized text style, or a complex screen layout, you are creating or extending classes. Concepts like inheritance allow you to reuse code, while mixins provide a powerful way to add functionality to classes without the limitations of single inheritance. Mastering Dart OOP will make you a much more efficient Flutter developer.

How To Study This Chapter

Focus on the syntax of constructors—Dart has some unique and very concise ways to initialize objects. Pay close attention to how super is used in inheritance, as you'll see this constantly in Flutter widget classes. Practice creating small class hierarchies in DartPad to see how methods and properties are passed down.

Functions in Dart

Before we get to classes, let's look at functions, as they are the "actions" that classes perform.

// Basic function
int add(int a, int b) {
  return a + b;
}

// Arrow syntax for single-line functions
int multiply(int a, int b) => a * b;

// Optional named parameters
void greet({String? name, String message = "Hello"}) {
  print('$message, $name');
}

void main() {
  greet(name: 'Bob'); // Output: Hello, Bob
}

Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.

class User {
  String name;
  int age;

  // Constructor with 'this' shorthand
  User(this.name, this.age);

  // Named constructor
  User.guest() : name = 'Guest', age = 0;

  void sayHello() {
    print('My name is $name and I am $age years old.');
  }
}

void main() {
  var user1 = User('Alice', 30);
  user1.sayHello();

  var guest = User.guest();
  guest.sayHello();
}

Inheritance

Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass).

class Person {
  String name;
  Person(this.name);

  void walk() => print('$name is walking');
}

class Developer extends Person {
  String language;

  // Using 'super' to call parent constructor
  Developer(String name, this.language) : super(name);

  void code() => print('$name is coding in $language');
}

Mixins

Mixins are a way of reusing a class's code in multiple class hierarchies.

mixin Logger {
  void log(String message) => print('LOG: $message');
}

class Database with Logger {
  void save() {
    log('Saving to database...');
  }
}

Abstract Classes

Abstract classes cannot be instantiated. They serve as templates for other classes.

abstract class Shape {
  double get area; // Abstract getter
}

class Circle extends Shape {
  double radius;
  Circle(this.radius);

  @override
  double get area => 3.14 * radius * radius;
}

With these OOP concepts in hand, you're ready to start building UIs with Flutter!