Dart Lesson 23 – Constructors | Dataplexa

Constructors in Dart

In this lesson, you will learn about constructors in Dart. Constructors are special methods used to initialize objects when they are created.

Constructors help you assign values to object properties automatically, making your code cleaner, safer, and more professional.


What Is a Constructor?

A constructor is a special function that:

  • Has the same name as the class
  • Runs automatically when an object is created
  • Initializes object properties

Every Dart class has a constructor — even if you don’t write one.


Default Constructor (Implicit)

If you do not define a constructor, Dart provides a default constructor automatically.

class User {
  String name = '';
  int age = 0;
}

void main() {
  User u1 = User();
  u1.name = 'Ravi';
  u1.age = 30;

  print(u1.name);
  print(u1.age);
}

Here, User() is the default constructor.


Creating a Custom Constructor

Let’s create a constructor that accepts values at object creation time.

class Employee {
  String name;
  String role;
  double salary;

  Employee(this.name, this.role, this.salary);

  void showDetails() {
    print('Name: $name');
    print('Role: $role');
    print('Salary: $salary');
  }
}

This constructor automatically assigns values to class properties.


Creating Objects Using Constructor

Now we create objects using the constructor.

void main() {
  Employee emp1 = Employee('Anil', 'Developer', 60000);
  Employee emp2 = Employee('Sunita', 'Designer', 52000);

  emp1.showDetails();
  emp2.showDetails();
}

Why Use Constructors?

Constructors ensure that objects are created in a valid state.

  • Prevents missing values
  • Makes code cleaner
  • Improves readability
  • Reduces runtime errors

Named Constructors

Dart allows multiple constructors using named constructors. These are useful for different initialization scenarios.

class Product {
  String name;
  double price;

  Product(this.name, this.price);

  Product.discounted(this.name) : price = 0.0;

  void showProduct() {
    print('Product: $name');
    print('Price: $price');
  }
}

Using both constructors:

void main() {
  Product p1 = Product('Laptop', 75000);
  Product p2 = Product.discounted('Keyboard');

  p1.showProduct();
  p2.showProduct();
}

Constructor with Validation Logic

Constructors can contain logic to validate data.

class BankAccount {
  String holder;
  double balance;

  BankAccount(this.holder, double amount)
      : balance = amount < 0 ? 0 : amount;

  void showAccount() {
    print('Holder: $holder');
    print('Balance: $balance');
  }
}

📝 Practice Exercises


Exercise 1

Create a class Car with properties brand, model, and price. Initialize them using a constructor.

Exercise 2

Create two car objects with different values.

Exercise 3

Add a method to display car details.


✅ Practice Answers


class Car {
  String brand;
  String model;
  double price;

  Car(this.brand, this.model, this.price);

  void showCar() {
    print('Brand: $brand');
    print('Model: $model');
    print('Price: $price');
  }
}

void main() {
  Car c1 = Car('Toyota', 'Innova', 2800000);
  Car c2 = Car('Tesla', 'Model 3', 4500000);

  c1.showCar();
  c2.showCar();
}

What’s Next?

In the next lesson, you will learn about Inheritance in Dart — how one class can reuse and extend the functionality of another class.