Dart constructor
A distinct kind of function that is created under the same name as its class is called a constructor. When an object is formed, its initialization is done using the constructor. Upon creating a class object, the constructor is invoked automatically. Although it lacks an explicit return type, it is somewhat similar to class functions. The most versatile type of constructor that is used to generate a new instance of a class is called generative.
Declaring it within the class is an option. Every class has its own constructor, but the Dart compiler will automatically build a default constructor by sending the default value to the member variable if we forget to define it or don’t use it. The default constructor will not be used if we specify our own constructor.
Example
Assume that we will build an object for the class named Student in the manner described below.
Student std = new Student()
It called the Student class’s default constructor.
Creating Constructor in Dart
As previously established, a constructor returns null, and its name is the same as the name of its class. Assume that the constructor name should likewise be Student if we have a class called Student.
Syntax
class ClassName {
ClassName() {
}
}
When building a constructor, the following two guidelines need to be kept in mind.
- The class name and constructor name ought to match.
- There isn’t an explicit return type in the constructor.
Example
void main() {
// Creating an object
Student std = new Student("Jones",26);
}
class Student{
// Declaring a construstor
Student(String str, int age){
print("The name is: ${str}");
print("The age is: ${age}");
}
}
Output
The name is: Jones
The age is: 26
Explanation
The constructor method Student(), which is the same as the class name, was constructed in the example above. We gave two parameters to the constructor, and the constructor was automatically called and the result printed when we constructed an object of the Student class and passed a value.
Types of Constructors
In Dart, constructors come in three different varieties, as listed below.
- Default Constructor or no-arg Constructor
- Parameter Constructor
- Named Constructor
Default Constructor or no-arg Constructor
Default constructors, often known as no-arg constructors, are constructors that do not have any parameters. If we don’t declare it in the class, the Dart compiler creates it automatically (without an argument). Whether we create a constructor with an argument or without one, the Dart compiler ignores the default constructor. Below is the syntax.
Syntax
class ClassName {
ClassName() {
// constructor body
}
}
Example
void main() {
// Call constructor automatically when we creates an object
Student std = new Student();
}
class Student{
// Declaring a construstor
Student(){
print("The example of the default constructor");
}
}
Output
The example of the default constructor
Parameterized Constructor
Additionally, we have the option of passing the parameters to a constructor known as a parameterized constructor. Instance variables are initialized using it. We occasionally require a constructor that takes one or more parameters. The primary purpose of parameterized constructors is to initialize instance variables with user-supplied values. Below is the syntax.
Syntax
class ClassName {
ClassName(parameter_list)
// constructor body
}
Example
void main() {
// Creating an object
Student std = new Student("Jones",26);
}
class Student{
// Declaring a parameterized constructor
Student(String str, int age){
print("The name is: ${str}");
print("The age is: ${age}");
}
}
Output
The name is: Jones
The age is: 26
Explanation
We defined a parameterized constructor with two parameters—name and age—in the example above. We constructed an object of the Student class and gave the constructor the correct value. The name and age were displayed on the screen as an output.
Named Constructors
Multiple constructors can be declared in a single class using the named constructors. Below is the syntax.
Syntax
className.constructor_name(param_list)
Example
void main() {
// Creating an object
Student std1 = new Student(); // object for Default constructor
Student std2 = new Student.namedConst("Computer Science"); // object for parameterized constructor
}
class Student{
// Declaring a construstor
Student(){
print("The example of the named constructor");
}
// Second constructor
Student.namedConst(String branch){
print("The branch is: ${branch}");
}
}
Output
The example of the named constructor
The branch is: Computer Science