Leave a rating/review
When we define a constructor in Dart, we define the initialization followed by any code that we want to want run.
You can view a constructor that has a header and a body. Now, when we define a class that has non-nullable undefined instance variables, all those properties must be initialized before the constructor body.
If you are coming from a language like Java or Swift, this may seem confusing whereby you initialize your properties in the constructor. With Dart, we initialize all our properties BEFORE the constructor body. In Dart, the constructor body is used to run some code after the initialization is complete.
Right now, the only way to initialize non-initialized properties is to pass them into the constructor. This is not an ideal solution. Dart also provides initialization lists.
An initialization list comes after the constructor parameter so we define the parameters first after which, we add our initialization list.
The list comes after the closing parenthesis and we must set any property that requires a value. We can use the values passed into the constructor or of course set our own. Let’s get stated working with initialization lists.
To get started, open up DartPad. We’re going to return to our user class so let’s redefine it here.
class User {
final int id;
final String name;
User(this.id, this.name);
}
Here we have our class that takes in an id and a name. We’ve also defined a constructor that passes in those values. Let’s add another uniqueId field that is a combination of both the id and the name. Add the following:
final String uniqueId;
We’ve defined our uniqueId and since it requires a value, we have to initialize it. We can do this in our our initialization list. Add the following:
User(this.id, this.name) : uniqueId = '$id$name';
We’ve defined our uniqueId variable using both the id and the name being passed into it. We simply converted the id (which is an int) into a string. On an aside, remember when I said almost everything in Dart is an object? That’s very much true with numbers and strings. They both have their own methods.
Let’s change the following:
User(this.id, this.name) : uniqueId = id.toString() + name;
This does the same exact thing. It converts the int to string and adds it to the name. We can even do something like this:
print(100.toString());
In a previous course, I had you check to see if a number was even based on the modulo operator. There’s actually a property for that.
print(43.isEven)
Yeah, so Dart is a cool language. Okay, let’s create a new user. Replace the code in main with the following:
var user = User(42, 'Ray');
Now print out the unique id.
print(user.uniqueId);
It prints out 42ray - all created from our initialization list. The initialization list is part of the construction which means after it, we can provide some custom code as part of our constructor. Add the following:
User(this.id, this.name) : uniqueId = id.toString() + name {
print(uniqueId);
}
Now we are just creating a user. Run the program. We get the unique id because the print statement is now included with the constructor. Awesome job.!