In the world of Programming Languages and OOPS, a construct is a special method that is used for various purposes.
So today in this blog, we will learn about the Constructor in Dart and how we can use these constructors.
What is a Constructor?
A constructor is a special method that is used for creating and initializing the objects of a class. Furthermore, We can also use a constructor for initializing the values to the class properties.
It can also perform any necessary setup before the execution of any method. You can define and call the Constructor in Dart language like below:-
1 2 3 4 5 6 7 8 9 10 11 |
class Constructor{ Constructor(){ print("Dart Constructor Initialized"); } } // Calling/Initializing the constructor void main (){ Constructor object = Constructor(); } |
Output:- Dart Constructor Initialized
Learn more about our Flutter Development Services.
Types of Constructors in Dart
There are several types of constructors in the Dart programming language:-
1. Default Constructor
As the name suggests, if no constructor is defined in a class, Dart automatically creates the constructor thus known as the default constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class DefaultConstructor{ String? name; DefaultConstructor(){ name = "Mobikul Blog"; } } void main (){ DefaultConstructor obj = DefaultConstructor(); // Calling/Initializing the DefaultConstructor print(obj.name); } // Output: Mobikul Blog |
2. Parameterized Constructor
It allows the creation of constructors with parameters for initializing the variables of the class at the time of object creation.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class ParameterizedConstructor{ String? name; ParameterizedConstructor({this.name}){ name = this.name; } } void main (){ ParameterizedConstructor obj = ParameterizedConstructor(name: "Mobikul Blog"); print(obj.name); } // Output:- Mobikul Blog |
3. Named Constructor:
Named constructors are the additional constructions provided in the Dart programming.
These are the constructors with names that you define within the class and allow you to create multiple constructors in a class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class NamedConstructor{ String? number; NamedConstructor({this.number}){ print("This is default constructor: ${this.number}"); } NamedConstructor.newConstructor({this.number}){ print("This is a named constructor: ${this.number}"); } NamedConstructor.newConstructor2({this.number}){ print("This is a named constructor: ${this.number}"); } } void main (){ NamedConstructor obj = NamedConstructor(number: "Zero"); NamedConstructor obj1 = NamedConstructor.newConstructor(number: "One"); NamedConstructor obj2 = NamedConstructor.newConstructor2(number: "Two"); } //Output: This is default constructor: Zero This is a named constructor: One This is a named constructor: Two |
Why use Constructors in Dart?
In advanced flutter or dart programming, the constructor serves several important purposes:-
1. Flexibility in Object Initialization:
In Dart, constructors are used for creating the objects of the classes. It allows the creation of objects with or without parameters, enabling different ways to initialize the objects and their uses.
2. Multiple Initialization:
The constructor in Dart can be initialized multiple times with different purposes for each initialization. These named constructors can have unique sets of parameters for specific constructors.
3. Maintainability and Readability:
With the proper use of Dart constructors, we can enhance code readability and maintainability.
4. Factory Constructor:
If you need to perform some logic that cannot be expressed in the initializer list, create a factory constructor.
A Factory Construct is a special feature provided by Dart that can add logic before returning the objects of the class.
Conclusion
So, In this article, we have learned various things about Dart constructors, their types and why should we use constructors in general programming.
You can also learn more about Flutter with Mobikul Blogs.