Constructors are special methods in Dart that are used to initialize objects. They are called when an instance of a class is created. Constructors can be used to set initial values for the object's properties or to perform any setup steps required for the object.

Key Concepts

  1. Default Constructor: A constructor that is automatically provided by Dart if no other constructors are defined.
  2. Named Constructor: Allows you to create multiple constructors for a class, each with a different name.
  3. Redirecting Constructor: Redirects to another constructor in the same class.
  4. Constant Constructor: Used to create compile-time constant objects.

Default Constructor

If you do not define any constructors in your class, Dart provides a default constructor that has no parameters and does nothing.

class Person {
  String name;
  int age;
}

void main() {
  var person = Person(); // Calls the default constructor
  person.name = 'Alice';
  person.age = 30;
  print('Name: ${person.name}, Age: ${person.age}');
}

Custom Constructor

You can define your own constructor to initialize the properties of the class.

class Person {
  String name;
  int age;

  // Custom constructor
  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

void main() {
  var person = Person('Alice', 30);
  print('Name: ${person.name}, Age: ${person.age}');
}

Named Constructor

Named constructors allow you to create multiple constructors for a class, each with a different name.

class Person {
  String name;
  int age;

  // Named constructor
  Person.withName(this.name) {
    age = 0; // Default age
  }

  // Another named constructor
  Person.withAge(this.age) {
    name = 'Unknown'; // Default name
  }
}

void main() {
  var person1 = Person.withName('Alice');
  var person2 = Person.withAge(30);
  print('Person1 - Name: ${person1.name}, Age: ${person1.age}');
  print('Person2 - Name: ${person2.name}, Age: ${person2.age}');
}

Redirecting Constructor

A redirecting constructor calls another constructor in the same class.

class Person {
  String name;
  int age;

  // Main constructor
  Person(this.name, this.age);

  // Redirecting constructor
  Person.withName(String name) : this(name, 0);

  // Another redirecting constructor
  Person.withAge(int age) : this('Unknown', age);
}

void main() {
  var person1 = Person.withName('Alice');
  var person2 = Person.withAge(30);
  print('Person1 - Name: ${person1.name}, Age: ${person1.age}');
  print('Person2 - Name: ${person2.name}, Age: ${person2.age}');
}

Constant Constructor

Constant constructors are used to create compile-time constant objects. They are defined using the const keyword.

class Point {
  final int x;
  final int y;

  // Constant constructor
  const Point(this.x, this.y);
}

void main() {
  var point1 = const Point(1, 2);
  var point2 = const Point(1, 2);

  print(point1 == point2); // true, because they are compile-time constants
}

Practical Exercises

Exercise 1: Create a Custom Constructor

Create a class Car with properties make, model, and year. Define a custom constructor to initialize these properties.

class Car {
  String make;
  String model;
  int year;

  // Custom constructor
  Car(this.make, this.model, this.year);
}

void main() {
  var car = Car('Toyota', 'Corolla', 2020);
  print('Make: ${car.make}, Model: ${car.model}, Year: ${car.year}');
}

Exercise 2: Implement Named Constructors

Extend the Car class to include named constructors Car.withMake and Car.withModel.

class Car {
  String make;
  String model;
  int year;

  // Main constructor
  Car(this.make, this.model, this.year);

  // Named constructor
  Car.withMake(this.make) {
    model = 'Unknown';
    year = 0;
  }

  // Another named constructor
  Car.withModel(this.model) {
    make = 'Unknown';
    year = 0;
  }
}

void main() {
  var car1 = Car.withMake('Toyota');
  var car2 = Car.withModel('Corolla');
  print('Car1 - Make: ${car1.make}, Model: ${car1.model}, Year: ${car1.year}');
  print('Car2 - Make: ${car2.make}, Model: ${car2.model}, Year: ${car2.year}');
}

Exercise 3: Use a Redirecting Constructor

Modify the Car class to include a redirecting constructor Car.withYear.

class Car {
  String make;
  String model;
  int year;

  // Main constructor
  Car(this.make, this.model, this.year);

  // Redirecting constructor
  Car.withYear(int year) : this('Unknown', 'Unknown', year);
}

void main() {
  var car = Car.withYear(2020);
  print('Make: ${car.make}, Model: ${car.model}, Year: ${car.year}');
}

Summary

In this section, you learned about constructors in Dart, including default constructors, custom constructors, named constructors, redirecting constructors, and constant constructors. Constructors are essential for initializing objects and setting up their initial state. By mastering constructors, you can create more flexible and robust classes in your Dart programs.

© Copyright 2024. All rights reserved