Dung (Donny) Nguyen

Senior Software Engineer

Factory Constructor in Dart

In Dart, a factory constructor is a special type of constructor used for creating objects, but with additional flexibility compared to normal constructors. Here’s an explanation of factory constructors and the differences between them and normal constructors:


Factory Constructor


Syntax of a Factory Constructor

class MyClass {
  final int value;

  // Private constructor
  MyClass._(this.value);

  // Factory constructor
  factory MyClass(int value) {
    if (value > 0) {
      return MyClass._(value); // Create a new instance
    } else {
      return MyClass._(0); // Return a default instance
    }
  }
}

void main() {
  var obj1 = MyClass(10); // Creates a new instance
  var obj2 = MyClass(-5); // Returns a default instance
}

Differences Between Factory Constructor and Normal Constructor

Feature Normal Constructor Factory Constructor
Keyword Declared without the factory keyword. Declared using the factory keyword.
Instance Creation Always creates a new instance of the class. May return an existing instance or create a new one.
Return Type Implicitly returns the class instance. Can return any subclass or cached instance.
Use Case Suitable for simple object initialization. Useful for instance reuse, caching, or returning specific implementations.
Inheritance Behavior Normal constructors are inherited by default. Factory constructors are not inherited.

Example of Caching with a Factory Constructor

class Singleton {
  static Singleton? _instance;

  // Factory constructor for singleton pattern
  factory Singleton() {
    return _instance ??= Singleton._internal();
  }

  // Private named constructor
  Singleton._internal();

  void showMessage() {
    print("Singleton instance");
  }
}

void main() {
  var s1 = Singleton();
  var s2 = Singleton();

  print(s1 == s2); // true, both refer to the same instance
}

When to Use Factory Constructor

If you need flexibility in creating and managing instances, factory constructors are the right tool for the job.