Dart Constructor Methods
Constructor Methods in Dart
In Dart, constructors are special methods that are used to initialize objects when they are created. A constructor is called automatically when an object is created from a class. Constructors can be used to set the initial values of an object's properties and perform any setup required.
Types of Constructors in Dart:
- Default Constructor: This is a constructor that is automatically provided if you do not define any constructor in your class. It is used to initialize the object with default values.
- Named Constructor: Dart allows you to define named constructors, which are useful when you need to create multiple ways of initializing an object with different arguments.
- Parameterized Constructor: This type of constructor allows you to pass arguments to initialize the object's properties when the object is created.
- Constant Constructor: If you want to create immutable objects (objects whose values cannot be changed after creation), Dart provides constant constructors.
Example of Constructors in Dart
Let’s explore the different types of constructors with examples.
1. Default Constructor:
If you don’t define any constructor in the class, Dart automatically provides a default constructor.
Output:
In this case, we didn’t define a constructor, but Dart provided a default one. The object is initialized with default values, and we set the name
and age
fields later.
2. Parameterized Constructor:
A parameterized constructor is used when you want to initialize an object with specific values at the time of creation.
Output:
Here, the constructor Person(this.name, this.age)
initializes the object's name
and age
directly when the object is created.
3. Named Constructor:
Named constructors allow you to have multiple constructors with different names in a class, making it possible to provide multiple ways of initializing objects.
Output:
In this example, Person.withNameOnly
is a named constructor that initializes the name
but gives a default value to age
. You can use multiple named constructors in a class.
4. Constant Constructor:
A constant constructor is used when you want to create objects that are compile-time constants. This is especially useful for creating immutable objects.
Output:
Here, the constructor is defined as const Point(this.x, this.y)
, and objects created with const
are compile-time constants. The identical
function checks whether point1
and point2
refer to the same object. Since both objects are constants with the same values, they refer to the same memory location.
Constructor Chaining (Calling One Constructor from Another)
Dart allows constructor chaining, where one constructor calls another constructor within the same class.
Output:
In this example, the Rectangle.square
constructor calls the main constructor of the Rectangle
class using : width = size, height = size;
.
Constructor Methods Summary:
- Default Constructor: Automatically provided by Dart if no constructor is defined.
- Parameterized Constructor: Allows initialization of an object with parameters.
- Named Constructor: Provides multiple ways to initialize an object with different initialization strategies.
- Constant Constructor: Used to create compile-time constant objects.
- Constructor Chaining: Allows one constructor to call another constructor within the same class.
Constructors are essential for object initialization and provide flexibility in how objects are created and initialized in Dart. By using various types of constructors, you can easily handle different initialization needs in your classes.