Types of construtors in dart
Despite the fact that a lot of developers and roadmaps recommend learning dart principles first and then moving on to flutter (since this is a dart framework and uses dart principles), many developers prefer learning both simultaneously to be able to build mobile apps.
This article addresses one of the Dart concepts, the constructors. There are some similar aspects (about constructors) to languages like java and c++ (if you come from them) and there are completely different aspects too.
So, Let’s get started…
Type #1 — Initializing the class members inside the constructor body
This constructor is similar to the one we use in other programming languages such as Java and C++, because we initialize our class members inside the constructor body. Using this type, you have to either declare the class members as late
, late final
or nullable
types:
declaring class members as late
type:
declaring class members as late final
type:
declaring class members as nullable
type:
But what if this class members types get in your way?
Then, maybe the most common type helps you. That’s where our second constructor type comes in.
Type #2: Initializing the members before the constructor body
This means that before our constructor body, we already have our class members initialized.
This syntax is really good because you don’t have to declare your members as late
, late final
or nullable
to be able to initialize them at the constructor.
Notice that we are using the keyword
this
in the parameters, because we are using the same members names, and that’s where the magic happens. If you use a different name (or not usingthis
keyword) the compiler won’t recognize it as a member initialization.
If you decide not to have a body in your constructor, just end the constructor declaration with a semicolon like in the code above.
Avoid this:
it’s better to use semicolon instead of curly braces on empty bodies.
This is good:
Type #3: Named constructors and Factory Constructors
I added this two types in the same section because there is already a medium article that talks about this two constructors. Even so, I’m going to summarize about Named constructors because I’ll need them to explain the next constructor type.
Named constructor are like normal constructor, but with a name. If you come from a language like java, you can have multiples constructors with the same name but differing on the type and number of the parameters. Unfortunately, we cannot do that in Dart, that’s why we have named constructors to help us working in a similar way (having multiples constructors).
You can use a named constructor to set default values of certain instances of this class. For instance, the initial
named constructor that assigns the value 20
to all the created instances using this constructor.
Type #4 — Redirecting Constructors
As the name suggest, this type of constructors redirects to another constructor.
The body of redirecting constructors must be empty.
Yes! the redirecting constructor is similar to the named constructor, but it is more powerful because it can call the other constructor (avoiding code repetition). In this case, the redirecting constructor call the default constructor that creates an object with an age of zero (0) years old by default.
The
this(name,0)
representsPerson(this._name,this._age);
constructor we defined earlier.
Type #5: const constructors
Const constructors are used to assign values to final members, and a class that only has final members is called immutable class.
This constructor requires final members, if we pass it non final parameters the compiler can throw an error.
Instantiating the class using the const constructor:
Notice we used the const
keyword before the constructor name, that’s because it is required using it when instantiating the class using the const constructor. If we do not use the const
keyword, we will create an object but not using the const constructor.
this is wrong if we want to instantiate the class using the constant constructor:
Even though the
Person
constructor is not the const constructor we defined, we cannot reassign the member’s values because they are final
Conclusion
Having this types (and more), we have to able to choose the type that most satisfy our need when coding, although the most recommended and most used type is the type #2.
For instance, if you know that your class members will never change, you should declare them as final
members and using an const constructor to assign them.
Happy Coding.
If you have any doubts or you may thing something is missing, you can contact me on twitter or telegram. I would be happy to talk to you about it.
Everything is a Widget!