Understanding Types of Constructors Class in Dart
In Dart, constructors are special methods used to initialize objects of a class. There are different types of constructors you can use, each with its unique purpose. In this blog, we’ll explore these constructors with simple examples for beginners. 🌟
1. Default Constructor
A default constructor is automatically created by Dart if you don't define any constructor in your class. It has no parameters and provides a basic way to create objects.
Example:
class Student {
String name = "Unknown";
int age = 0;
}
void main() {
Student student = Student(); // Using the default constructor
print("Name: ${student.name}, Age: ${student.age}");
}
Output:
Name: Unknown, Age: 0
Here, Dart automatically provides a default constructor because we didn’t define one.
2. Parameterized Constructor
A parameterized constructor allows you to pass values to initialize the instance variables of a class.
Example:
class Student {
String name;
int age;
// Parameterized constructor
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
print("Name: $name, Age: $age");
}
}
void main() {
Student student = Student("Alice", 20); // Passing values to the constructor
student.display();
}
Output:
Name: Alice, Age: 20
Here, we’re passing name and age as parameters to initialize the Student object.
3. Named Constructor
Named constructors allow you to create multiple constructors in the same class by giving each a unique name. This is useful when you want to initialize objects differently based on the situation.
Example:
class Student {
String name;
int age;
// Named constructor for regular students
Student.regular(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
print("Name: $name, Age: $age");
}
}
void main() {
Student regularStudent = Student.regular("Bob", 22);
regularStudent.display(); // Output: Name: Bob, Age: 22
}
4. Constructor with this Keyword
You can simplify the parameterized constructor by using the this keyword to assign parameter values to instance variables.
Example:
class Student {
String name;
int age;
// Constructor using 'this'
Student(this.name, this.age);
void display() {
print("Name: $name, Age: $age");
}
}
void main() {
Student student = Student("Charlie", 18);
student.display();
}
Output:
Name: Charlie, Age: 18
5. Redirecting Constructor
A redirecting constructor calls another constructor in the same class, avoiding code duplication.
Example:
class Student {
String name;
int age;
// Main constructor
Student(this.name, this.age);
// Redirecting constructor
Student.guest() : this("Guest", 0);
}
void main() {
Student guestStudent = Student.guest();
print("Name: ${guestStudent.name}, Age: ${guestStudent.age}");
}
Output:
Name: Guest, Age: 0
Summary of Constructor Types
| Constructor Type | Use Case |
| Default Constructor | Automatically created if no constructor is defined. |
| Parameterized Constructor | Pass values to initialize instance variables. |
| Named Constructor | Create multiple constructors with unique names. |
Constructor with this | Simplifies parameterized constructors. |
| Factory Constructor | Control object creation (e.g., Singleton pattern). |
| Redirecting Constructor | Call another constructor within the same class. |
Practice Challenge
Create a Book class with:
A parameterized constructor to initialize
titleandauthor.A named constructor for an unknown book with the title "Unknown" and author "Anonymous."
A method to display book details.
Test both constructors in the main function! 💪
With these constructor types in your toolkit, you’re now equipped to handle various object initialization scenarios in Dart. Keep practicing and experimenting! 😊

