Constructors in Dart
A constructor is a special method that is used to initialize objects. The name of the constructor is the same as the name of the class.
Default Constructor in Dart
A default constructor is a constructor that has no parameters. Here is an example of a default constructor in Dart:
class Person {
String name;
int age;
Person() {
print("Default Constructor");
}
}
Note: If you don't define a constructor in a class, then Dart creates a default constructor for you.
Parameterized Constructor in Dart
A parameterized constructor is a constructor that has parameters. Here is an example of a parameterized constructor in Dart:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Note: You can also use the this
keyword to refer to the current instance of the class. Here is an example of a parameterized constructor using the this
keyword in Dart:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Named Constructor in Dart
A named constructor is a constructor that has a name. Here is an example of a named constructor in Dart:
class Person {
String name;
int age;
Person.namedConstructor(String name, int age) {
this.name = name;
this.age = age;
}
}
Note: To define a named constructor you need to use the name of the class followed by a dot (.) and the name of the constructor.
Constant Constructor in Dart
A constant constructor is a constructor that creates a compile-time constant. Here is an example of a constant constructor in Dart:
class Person {
String name;
int age;
const Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Note: To define a constant constructor you need to use the const
keyword before the name of the constructor. This type of constructors are used when you want to create a compile-time constant which value must not change.
factory Constructor
A factory constructor is a constructor that returns an instance of the class. Here is an example of a factory constructor in Dart:
class Person {
String name;
int age;
factory Person(String name, int age) {
return Person.namedConstructor(name, age);
}
Person.namedConstructor(String name, int age) {
this.name = name;
this.age = age;
}
}
Note: To define a factory constructor you need to use the factory
keyword followed by the name of the class. factory constructors are used when you want to return an instance of the class from a method.
Use case of factory constructor
There are many use cases of factory constructors. However, the frequent one is while working with backends and apis.
When we make a request to the backend, we get a json response. There is a concepts of converting datas to class models modern day programming. We we take a look at that in the lesson ***Flutter and Backend Intergration
***.
What that means is we get the data from APIs and we need to convert them to class models. However, if there are any required fields in the class model, we can't create the objects without passing that field. In this scenario, factory
constructor comes to the rescue.
Let's take a look at an example:
class Person {
String name;
int age;
factory Person.fromJson(String name, int age) {
this.name = name;
this.age = age;
}
}
Let's assume the json response from the backend is:
{
"name": "John",
"age": 25
}
If we try to create an object of the class Person
without passing the name
and age
fields, we will get an error. This can be done making the method static
or using the factory
constructor ( recommended ). We will learn about static
keyword in the lessons ahead. Now let's make use of the factory
constructor to create an object of the class Person
without passing the name
and age
fields:
/// Note that name, and age are parameters of the factory constructor, not the class constructor.
/// At this point, we would have the data that we got from the backend.
final data = {
"name": "John",
"age": 25
};
/// lets pass the arugments to the factory constructor.
final person = Person.fromJson(data["name"], data["age"]);
/// Now we have the object of the class Person.
print(person.name); /// John
print(person.age); /// 25
Output
John
25
I hope you understood the use case of the factory
constructor. Let's move forward.
Defining the different types of parameters in Dart Class Constructors
Parameters are the variables that are passed to the constructor. They are curical in the process of creating objects. Let's learn about the different types of parameters in Dart Class Constructors.
Positioned Parameters in Dart Class Constructors
Positioned parameters are the parameters that are passed to the constructor in the same order as they are defined in the constructor. Here is an example of a class constructor with positioned parameters in Dart:
class Person {
String name;
int age;
Person(String name, int age);
}
Note: The parameters name
and age
are positioned parameters.
Optional Parameters in Dart Class Constructors
Optional parameters are the parameters that are passed to the constructor in any order. Here is an example of a class constructor with optional parameters in Dart:
class Person {
String name;
int age;
Person({String name, int age}) ;
}
Note: The parameters name
and age
are optional parameters.
Named Parameters in Dart Class Constructors
Named parameters are the parameters that are passed to the constructor using their names. Here is an example of a class constructor with named parameters in Dart:
class Person {
String name;
int age;
Person({String name, int age});
}
Note: The parameters name
and age
are named parameters.
When you want any named constructor to be optional, you can use the ?
operator. Here is an example of a class constructor with optional named parameters in Dart:
class Person {
String name;
int age;
Person({String? name, int? age});
}
Note: The parameters name
and age
are optional named parameters.
When you want to make any named parameters required you just have to annoate it with the required
keyword. Here is an example of a class constructor with required named parameters in Dart:
class Person {
String name;
int age;
Person({required String name, required int age});
}
Note: The parameters name
and age
are required named parameters.
When you want to assign a default value inside a constructor, you can use the =
operator. Here is an example of a class constructor with default named parameters in Dart:
class Person {
String name;
int age;
Person({ this.name = "John", this.age = 25 });
}
Note: The parameters name
and age
are default named parameters. If no value is passed the value for name will be John
and the value for age will be 25
respectively.
Conclusion
In this lesson, we learned about the different types of constructors in Dart. We also learned about the different types of parameters in Dart Class Constructors.