Sitemap
Dart Tutorials

Dart Tutorials for beginners and advanced programmers

Constructor in Dart

2 min readMar 26, 2025

--

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.

Press enter or click to view image in full size

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.

--

--

Usama Sarwar
Usama Sarwar

Written by Usama Sarwar

Helping founders turn ideas into market-ready web and mobile apps within 30 days using Google’s Flutter & Next.js without juggling multiple teams.

Dart Tutorials
Dart Tutorials

Published in Dart Tutorials

Dart Tutorials for beginners and advanced programmers