Skybinary

View Categories

2. Learn Flutter Constructors and Named Constructors in Dart

1 min read

Flutter Constructors and Named Constructors in Dart #

Constructors play a vital role in Object-Oriented Programming by initializing objects when they are created. In Dart, constructors provide a clean and efficient way to assign initial values to class properties. Understanding constructors and named constructors is essential for writing structured, readable, and maintainable Dart and Flutter code.


What Is a Constructor? #

A constructor is a special method that is automatically called when an object of a class is created. Its primary purpose is to initialize the properties of the class.

In Dart, a constructor has the same name as the class and does not have a return type.

Example:

class User {
  String name;
  int age;

  User(this.name, this.age);
}

void main() {
  User user = User('Ahmed', 24);
}

In this example:

  • User is the constructor.
  • this.name and this.age assign values to class properties.
  • The constructor runs automatically when the object is created.

Default Constructor #

If no constructor is defined, Dart automatically provides a default constructor with no parameters.

Example:

class Product {
  String name = '';
  double price = 0.0;
}

void main() {
  Product product = Product();
}

Here, Dart creates a default constructor because none is explicitly defined.


Parameterized Constructor #

A parameterized constructor allows passing values during object creation.

Example:

class Employee {
  String id;
  String department;

  Employee(this.id, this.department);
}

This approach reduces boilerplate code and improves readability.


Named Constructors #

Named constructors allow a class to have multiple constructors, each with a specific purpose. They are useful when objects need to be created in different ways.

Syntax:

ClassName.constructorName()

Example:

class Account {
  String type;
  double balance;

  Account(this.type, this.balance);

  Account.savings() 
      : type = 'Savings',
        balance = 0.0;

  Account.current(double initialBalance)
      : type = 'Current',
        balance = initialBalance;
}

Usage:

void main() {
  Account acc1 = Account.savings();
  Account acc2 = Account.current(5000.0);
}

Each named constructor clearly defines how the object is initialized.


Named Constructors for Validation and Logic #

Named constructors can include validation or custom initialization logic.

Example:

class Student {
  String name;
  int marks;

  Student(this.name, this.marks);

  Student.passed(this.name)
      : marks = 50;

  Student.failed(this.name)
      : marks = 0;
}

This approach improves clarity and ensures consistent object creation.


Constant Constructors #

Constant constructors are used to create compile-time constants. They are especially useful in Flutter for performance optimization.

Example:

class AppConfig {
  final String appName;
  final int version;

  const AppConfig(this.appName, this.version);
}

Objects created using const are immutable and memory-efficient.


Best Practices for Using Constructors in Dart #

  • Use concise constructor syntax with this where possible.
  • Prefer named constructors for multiple initialization scenarios.
  • Keep constructors simple and focused on initialization.
  • Use const constructors when objects are immutable.
  • Avoid complex business logic inside constructors.

Real-World Usage in Flutter #

  • Initializing widgets with required parameters.
  • Creating models from API responses using named constructors.
  • Defining different states of an object clearly.
  • Improving code readability and maintainability.

Constructors and named constructors are essential tools in Dart that enable clean object creation, flexible initialization, and professional application architecture. Mastering them will significantly improve your Flutter and Dart development skills.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top