Constructors
Constructors are special methods that are called when you create a new instance of a class. Think of them as the "setup crew" for your objects - they initialize the object's properties and get everything ready for use.
Default Constructor
Every class in Dart has a default constructor, even if you don't explicitly define one. It's like having an invisible helper that creates basic instances of your class.
class Person {
String name = '';
int age = 0;
}
void main() {
Person person = Person(); // Using default constructor
print(person.name); // Output: (empty string)
print(person.age); // Output: 0
}
Parameterized Constructor
You can create constructors that accept parameters to initialize your object with specific values right from the start.
class Person {
String name;
int age;
// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
void main() {
Person person = Person('Alice', 25);
print(person.name); // Output: Alice
print(person.age); // Output: 25
}
Shorthand Constructor Syntax
Dart provides a convenient shorthand syntax for constructors that just assign parameters to instance variables:
class Person {
String name;
int age;
// Shorthand constructor - much cleaner!
Person(this.name, this.age);
}
void main() {
Person person = Person('Bob', 30);
print('${person.name} is ${person.age} years old');
}
Named Constructors
Sometimes you need different ways to create objects. Named constructors let you create multiple constructors with descriptive names:
class Person {
String name;
int age;
// Default constructor
Person(this.name, this.age);
// Named constructor for creating a baby
Person.baby(String name) {
this.name = name;
this.age = 0;
}
// Named constructor for creating an adult
Person.adult(String name) {
this.name = name;
this.age = 18;
}
}
void main() {
Person child = Person.baby('Charlie');
Person adult = Person.adult('Diana');
print('${child.name} is ${child.age} years old'); // Charlie is 0 years old
print('${adult.name} is ${adult.age} years old'); // Diana is 18 years old
}
Constructor with Optional Parameters
You can make constructor parameters optional using square brackets [] for positional parameters or curly braces {} for named parameters:
class Car {
String brand;
String model;
int year;
String color;
// Constructor with optional named parameters
Car(this.brand, this.model, {this.year = 2023, this.color = 'white'});
}
void main() {
Car car1 = Car('Toyota', 'Camry');
Car car2 = Car('Honda', 'Civic', year: 2022, color: 'blue');
print('${car1.brand} ${car1.model} (${car1.year}) - ${car1.color}');
print('${car2.brand} ${car2.model} (${car2.year}) - ${car2.color}');
}
Key Points to Remember
- Constructors have the same name as the class
- They don't have a return type
- Use
this.propertyshorthand for simple assignments - Named constructors help create objects in different ways
- Optional parameters make constructors more flexible
Constructors are your gateway to creating well-initialized objects. They ensure your objects start life with all the right values!