Sub topic 5
Generative Constructors and Initializing Formals
This chapter assumes basic Dart syntax, null safety, and familiarity with objects and methods. A generative constructor is a constructor that creates and initializes a new instance of its class. It is the normal constructor form in Dart.
How generative constructors work
A constructor has the same name as its class. It does not declare a return type. The constructor parameters provide the values needed to initialize the object's fields.
class Rectangle {
final double width;
final double height;
Rectangle(this.width, this.height);
Rectangle.square(double side)
: width = side,
height = side;
double get area => width * height;
}
void main() {
final rectangle = Rectangle(4, 3);
final square = Rectangle.square(5);
print(rectangle.area); // 12.0
print(square.area); // 25.0
}Rectangle(this.width, this.height) uses initializing formals. An initializing formal is a constructor parameter that assigns its value directly to an instance field. The shorter form is equivalent to writing:
Rectangle(double width, double height)
: width = width,
height = height;The explicit version is harder to read because each parameter has the same name as its field. Initializing formals make the relationship clear.
A generative constructor performs its work in this order:
flowchart TD
A[Call the constructor] --> B[Allocate a new object]
B --> C[Evaluate constructor arguments]
C --> D[Run the initializer list]
D --> E[Run the constructor body]
E --> F[Return the initialized object]Initializer lists and final fields
An initializer list is the part after a constructor's parameter list and before its body. It starts with a colon. Use it to initialize fields, calculate values, or validate arguments before the body runs.
This is especially important for final fields. A final field must receive its value during object initialization. You cannot assign it later in the constructor body.
class User {
final String name;
final int age;
User(this.name, this.age)
: assert(age >= 0);
User.guest()
: name = 'Guest',
age = 0;
User.adult(this.name)
: age = 18;
String describe() => '$name ($age)';
}
void main() {
final first = User('Mina', twentyFive());
final guest = User.guest();
final adult = User.adult('Jon');
print(first.describe());
print(guest.describe());
print(adult.describe());
}
int twentyFive() => 25;The User.adult constructor combines an initializing formal for name with an initializer-list assignment for age. The User.guest constructor initializes both fields directly.
> callout note > Initializer lists run before the constructor body. They can initialize fields, but they cannot use this to call ordinary instance methods before the object is fully initialized.
Choosing a constructor form
| Need | Suitable form |
|---|---|
| Copy parameters directly into fields | Initializing formals |
| Calculate or validate a field value | Initializer list |
| Offer a different way to create an object | Named generative constructor |
| Set a default value for an optional field | Optional initializing formal |
Use named constructors when each creation path has a clear meaning, such as User.guest() or Rectangle.square().
Exercise
Why can a final field be initialized in an initializer list but not assigned in the constructor body?
Show answer
The initializer list runs during object initialization, before the constructor body. Dart requires each final field to be assigned before the body begins.