September 2019
Beginner
512 pages
12h 52m
English
To instantiate a class, we use the new keyword, followed by the corresponding constructor with parameters, if required. Now, let's change the Person class and define a constructor with parameters on it:
class Person { String firstName; String lastName; Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } String getFullName() => "$firstName $lastName";}main() { // Person somePerson = new Person(); this would not compile as we //defined mandatory parameters on constructor Person somePerson = new Person("Clark", "Kent"); print(somePerson.getFullName());}
The constructor is also a function in Dart and its role is to initialize the instance of the class properly. As a function, it ...
Read now
Unlock full access