Constant Constructor in Dart

Constant Constructor In Dart

Constant constructor is a constructor that creates a constant object. A constant object is an object whose value cannot be changed. A constant constructor is declared using the keyword const.

Info

Note: Constant Constructor is used to create a object whose value cannot be changed. It Improves the performance of the program.

Rule For Declaring Constant Constructor In Dart

  • All properties of the class must be final.
  • It does not have any body.
  • Only class containing const constructor is initialized using the const keyword.

Example 1: Constant Constructor In Dart

In this example below, there is a class Point with two final properties: x and y. The class also has a constant constructor that initializes the two properties. The class also has a method called display, which prints out the values of the two properties.

class Point {
  final int x;
  final int y;

  const Point(this.x, this.y);
}

void main() {
  // p1 and p2 has the same hash code.
  Point p1 = const Point(1, 2);
  print("The p1 hash code is: ${p1.hashCode}");

  Point p2 = const Point(1, 2);
  print("The p2 hash code is: ${p2.hashCode}");
  // without using const
  // this has different hash code.
  Point p3 = Point(2, 2);
  print("The p3 hash code is: ${p3.hashCode}");

  Point p4 = Point(2, 2);
  print("The p4 hash code is: ${p4.hashCode}");
}

Show Output
Run Online

Info

Note: Here p1 and p2 has the same hash code. This is because p1 and p2 are constant objects. The hash code of a constant object is the same. This is because the hash code of a constant object is computed at compile time. The hash code of a non-constant object is computed at run time. This is why p3 and p4 have different hash code.

Example 2: Constant Constructor In Dart

In this example below, there is a class Student with three properties: name, age, and rollNumber. The class has one constructor. The constructor is used to initialize the values of the three properties. We also have an object of the class Student called student.

class Car {
  final String? name;
  final String? model;
  final int? prize;

  // Constant Constructor
  const Car({this.name, this.model, this.prize});
}

void main() {
  // Here car is object of class Car.
  const Car car = Car(name: "BMW", model: "X5", prize: 50000);
  print("Name: ${car.name}");
  print("Model: ${car.model}");
  print("Prize: ${car.prize}");
}

Show Output
Run Online

Example 3: Constant Constructor With Named Parameters In Dart

In this example below, there is a class Car with three properties: name, model, and prize. The class has one constructor. The constructor is used to initialize the values of the three properties. We also have an object of the class Car called car.

class Car {
  final String? name;
  final String? model;
  final int? prize;

  // Constant Constructor
  const Car({this.name, this.model, this.prize});
}

void main() {
  // Here car is object of class Car.
  const Car car = Car(name: "BMW", model: "X5", prize: 50000);
  print("Name: ${car.name}");
  print("Model: ${car.model}");
  print("Prize: ${car.prize}");
}

Show Output
Run Online

Benefits Of Constant Constructor In Dart

  • Improves the performance of the program.

Challenge

Create a class Student with three properties: name, age, and rollNumber. The class should have one constant constructor. The constructor should initialize the values of the three properties. Create an object of the class Student and print the values of the three properties.

Video

Watch our video on constant constructor in Dart.