Summary: in this tutorial, you’ll learn how to use Dart constructor to create and initialize objects of a class.
A constructor is a special method for creating and initializing objects of a class. The constructor is automatically called when an object of the class is created.
Default constructor
When you define a class without explicitly providing a constructor, the class will have a default constructor.
A default constructor has the same name as the class name. Also, it has no parameters and returns an instance of the class.
For example, the following defines a Point
class that has two properties x
and y
:
class Point {
int x = 0;
int y = 0;
}
Code language: Dart (dart)
It’s equivalent to the following Point
class with a default constructor:
class Point {
int x = 0;
int y = 0;
Point();
}
Code language: Dart (dart)
A constructor is special because Dart will automatically call it when you create a new object. For example:
class Point {
int x = 0;
int y = 0;
Point() {
print('Constructor was called.');
}
}
void main() {
var p1 = Point();
}
Code language: Dart (dart)
Output:
Constructor was called.
Code language: Dart (dart)
How it works.
First, add a statement that displays a message in the default constructor of the Point
class:
Point() {
print('Constructor was called');
}
Code language: Dart (dart)
Second, create a new instance of the Point
class. The constructor was automatically called that displayed a message:
var p1 = Point();
Code language: Dart (dart)
Custom constructor
In practice, you often want to create an object and initialize its properties at the same time. To do it, you can use a custom constructor.
The name of the custom constructor is the same as the class name. Also, a custom constructor has parameters like a function. For example:
class Point {
int x = 0;
int y = 0;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Code language: Dart (dart)
In this example, the custom constructor of the Point
class has two parameters x
and y
. Inside the constructor, we assign the parameters to the properties of the Point
class.
Since parameters have the same names as properties, you need to use the this
keyword to explicitly reference the x
and y
properties.
Because the Point
has a custom constructor, you need to pass arguments to it when creating a new object:
var p1 = Point(10, 20);
Code language: Dart (dart)
Put it all together.
class Point {
int x = 0;
int y = 0;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
void main() {
var p1 = Point(10, 20);
}
Code language: Dart (dart)
Dart calls the following constructor as a long-form constructor:
Point(int x, int y) {
this.x = x;
this.y = y;
}
Code language: Dart (dart)
Also, Dart has a short-form constructor where you don’t provide the body. Instead, you list the properties that you want to initialize like this:
Point(this.x, this.y);
Code language: Dart (dart)
In the short-form constructor, Dart infers the types of parameters as integers. The short-form constructor works like the long-form constructor but the code is more concise:
class Point {
int x = 0;
int y = 0;
Point(this.x, this.y);
}
void main() {
var p1 = Point(10, 20);
}
Code language: Dart (dart)
Named constructor
A named constructor is a constructor that has a name. To define a named constructor, you add an identifier to the class name with the following syntax:
className.constructorName()
Code language: Dart (dart)
The following defines a named constructor called origin
:
Point.origin() {
this.x = 0;
this.y = 0;
}
Code language: Dart (dart)
The origin constructor initializes x
and y
properties to zero. To call the origin constructor, you use the following:
Point.origin();
Code language: Dart (dart)
The following shows the complete Point
class and calls the named constructor to create a new Point
object:
class Point {
int x = 0;
int y = 0;
Point(this.x, this.y);
Point.origin() {
this.x = 0;
this.y = 0;
}
}
Code language: Dart (dart)
Forwarding constructors
A constructor can call another constructor within the class. This is called redirect or forwarding. For example, you can call a custom constructor from the named constructor:
Point.origin() : this(0, 0);
Code language: Dart (dart)
In this example, the Point.origin()
constructor calls the Point(this.x, this.y)
constructor to initialize the x and y properties using the this
keyword.
The following shows the complete Point
class with short-form and redirecting constructors:
class Point {
int x = 0;
int y = 0;
Point(this.x, this.y);
Point.origin() : this(0, 0);
}
void main() {
var p1 = Point.origin();
}
Code language: Dart (dart)
It’s important to note that you can use the optional parameters and named parameters for constructors.
For example, the following makes the parameters of the Point
constructor the named constructor:
class Point {
int x = 0;
int y = 0;
Point({this.x = 0, this.y = 0});
Point.origin() : this(x: 0, y: 0);
}
void main() {
var p1 = Point(x: 10, y: 20);
}
Code language: Dart (dart)
Summary
- Use constructors to create and initialize objects.