Class Constructors

Learn about Dart constructors and their specific uses.

Since Dart is an object-oriented programming language, we should be familiar with its constructors. In this lesson, we’ll learn about constructors and look at various ways to build the constructors of a class.

Normal constructor

Press + to interact
class Student {
String name;
int age;
// constructor
Student(String name, int age) {
this.name = name;
this.age = age;
}
}

For those with a Java background, this will look familiar. Now let’s look at it in a more sugar-syntactic way:

Press + to interact
class Student {
String name;
int age;
Student(this.name, this.age);
}

Defining a constructor in this way is fine, and the Dart will mean it ...