In this chapter, we will learn about Dart constructors and how it is used within the class with the help of examples.
Dart Constructors
Constructors are used inside the class.
Constructors are a function that initializes an object when it is created.
When an object is created, the constructor is called by default.
For example,
If we have a class Person and we create an object
var student = new Person();
This invokes the default constructor of the Person class.
Creating Constructors
The syntax of creating constructors is:
class Class_Name {
Class_Name() {
// constructor body
}
}
Here, the first Class_Name is the class.
Class_Name() is a constructor
The constructor is defined with the same name as of the class.
Example: Creating Constructors
void main() {
var student = new Person('Sam');
}
class Person {
Person(String name) {
print("Name : ${name}");
}
}
Output
Name : Sam
In the above example, class Person is a class.
Person(String name) is a constructor that takes the string parameter. And the value is printed inside the constructor.
student is an object that passes a value Sam.
var student = new Person('Sam');
Constructors Types
There are three types of Dart constructors. They are:
- Default Constructor
- Parameter Constructor
- Named Constructor
Default Constructor
A default constructor has no parameter. If a constructor is not declared in a class, a default no-argument constructor is provided by the Dart compiler.
If a constructor is available, the compiler ignores the default constructor.
The syntax of default constructor is:
class Class_Name {
Class_Name() {
// constructor body
}
}
Example: Default Constructor
void main() {
var student = new Person();
}
class Person {
Person() {
print("Default constructor example.");
}
}
Output
Default constructor example.
The above example shows the default constructor with no parameters.
Parameter Constructor
The parameter constructor accepts single or multiple parameters. The parameters initialize the instance variables.
The syntax of parameter constructor is:
class Class_Name {
Class_Name(parameters) {
// constructor body
}
}
Example: Parameter Constructor
void main() {
var student1 = new Person('Sam', 22);
var student2 = new Person('Jerry', 23);
}
class Person {
Person(String name, int age) {
print("Name : ${name}");
print("Age: ${age}");
}
}
Output
Name : Sam
Age: 22
Name : Jerry
Age: 23
Name : Sam
Age: 22
Name : Jerry
Age: 23
In the above example, the constructor takes two parameters name and age.
Named Constructor
We can also use multiple constructors inside a single class through a named constructor.
The syntax of named constructor is:
className.constructor_name(param_list);
Example: Named Constructor
void main() {
// object
var student1 = new Person();
var student2 = new Person.namedConst("Sam");
}
class Person {
// constructor
Person() {
print("Named constructor example.");
}
//second constructor
Person.namedConst(String name) {
print("Name is: ${name}");
}
}
Output
Named constructor example.
Name is: Sam
In the above example, the second constructor takes the name parameter.
Person.namedConst(String name) {
print("Name is: ${name}");
}