Member-only story
Constructor in Dart
A constructor is a special method in a class that is used to create and initialize objects. It runs automatically when an object is instantiated.
Read Medium Story for free 👇
Types of Constructors in Dart
1. Default Constructor
If no constructor is explicitly defined, Dart provides a default constructor with no parameters.
class Car {
String brand = 'Unknown';
void display() {
print('Car brand: $brand');
}
}
void main() {
Car myCar = Car();
myCar.display(); // Output: Car brand: Unknown
}2. Parameterized Constructor
Allows passing values to initialize object properties.
class Car {
String brand;
int year;
Car(this.brand, this.year);
}
void main() {
Car myCar = Car('Toyota', 2022);
print(myCar.brand); // Output: Toyota
}3. Named Constructors
Used when multiple ways of initializing an object are needed.










