Dung (Donny) Nguyen

Senior Software Engineer

Constructor in Dart

In Dart, a constructor is a special method that is used to create and initialize an object of a class. When you create an instance of a class, the constructor sets up the initial state of that object.

Types of Constructors in Dart

  1. Default Constructor:
    • If you don’t define a constructor in a class, Dart provides a default constructor. It is a no-argument constructor that initializes all fields to their default values.
    class Animal {
      // Dart provides a default constructor here
    }
    
    void main() {
      var cat = Animal();
    }
    
  2. Parameterized Constructor:
    • You can define a constructor with parameters to initialize object properties with specific values.
    class Animal {
      String name;
      int age;
    
      // Parameterized constructor
      Animal(this.name, this.age);
    }
    
    void main() {
      var dog = Animal('Buddy', 3);
    }
    
  3. Named Constructors:
    • You can create multiple constructors in a class using named constructors. This is useful when you need different ways to initialize an object.
    class Animal {
      String name;
      int age;
    
      // Named constructor
      Animal.withName(this.name);
      Animal.withAge(this.age);
    }
    
    void main() {
      var cat = Animal.withName('Whiskers');
      var parrot = Animal.withAge(2);
    }
    
  4. Factory Constructors:
    • A factory constructor is a special type of constructor that can return an instance of a class or an existing instance, based on some logic. It is used when the creation process is complex or involves conditions.
    class Animal {
      String name;
    
      Animal._internal(this.name);
    
      // Factory constructor
      factory Animal(String name) {
        if (name == 'Dog') {
          return Animal._internal('Buddy');
        }
        return Animal._internal(name);
      }
    }
    
    void main() {
      var dog = Animal('Dog'); // Outputs: Buddy
    }
    

Constructors are essential for initializing the objects with desired properties and ensuring they are set up correctly.