What is a Constructor?
A constructor is a special method used to initialize an object when it is created. It has the same name as the class and no return type. Dart provides several types of constructors to give you flexibility in how objects are created and initialized.
- Default Constructor
If you don't define a constructor, Dart provides a default constructor with no arguments. It doesn't do anything but allows you to create an instance.
- Parameterized Constructor
You can define a constructor that takes parameters to set initial values. The most concise syntax uses this.property in the parameter list.
- Named Constructors
Named constructors allow a class to have multiple constructors with different names. They are useful for providing different ways to create an object.
- Redirecting Constructors
A redirecting constructor delegates its work to another constructor in the same class. It has an empty body and uses this followed by the target constructor.
- Constant Constructors
If a class produces objects that never change, you can make its constructor const. This allows the objects to be compile‑time constants, saving memory and improving performance.
All fields must be final for a constant constructor to work.
- Factory Constructors
A factory constructor doesn't always create a new instance. It can return an existing instance (e.g., from a cache) or an instance of a subtype. It uses the factory keyword.
Factory constructors are also commonly used to implement the singleton pattern or to perform complex initialization before returning an instance.
Initializer List
You can add an initializer list before the constructor body to initialize fields, assert conditions, or call super constructors. It's separated by a colon :.
Calling Superclass Constructors
By default, a constructor calls the superclass's default constructor. You can explicitly call a specific super constructor using the initializer list.
Common Mistakes
- Forgetting to initialize non‑nullable fields: Ensure every field gets a value either directly, in the constructor, or in the initializer list.
- Using
thisin initializer list before super call: You cannot referencethisin the initializer list until after the super constructor has been called.
- Using
- Overusing factory constructors: Only use them when you need to return an existing instance or perform logic before creation.
- Not making constant constructors when possible: If a class is immutable, consider adding a
constconstructor to allow compile‑time constants.
- Not making constant constructors when possible: If a class is immutable, consider adding a
Interview Questions
- Q: What is the difference between a named constructor and a factory constructor? A: A named constructor always creates a new instance of the class, while a factory constructor can return an existing instance or a subtype.
- Q: Can a class have both a default constructor and a named constructor? A: Yes, a class can have multiple constructors, including the default (if you don't define any) and named ones.
- Q: When would you use a constant constructor? A: When the class represents an immutable value and you want objects that can be compile‑time constants (e.g., for performance or canonicalization).
- Q: What is an initializer list used for? A: To initialize fields before the constructor body runs, assert conditions, or call a super constructor.
Key Takeaways
- Constructors initialize objects. They have the same name as the class and no return type.
- Dart supports default, parameterized, named, redirecting, constant, and factory constructors.
- Use initializer lists to set fields or assert conditions before the constructor body.
- Call superclass constructors using
super()in the initializer list.
- Call superclass constructors using
- Factory constructors give you control over instance creation (caching, singletons, etc.).
- Constant constructors (
const) create compile‑time constants and must have allfinalfields.
- Constant constructors (