In Dart, there’s a concise syntax for constructors with init formals. You can declare a simple class with constructor this way:
class Person {
String name;
int age;
Person(this.name, this.age);
}
By default, the type of the constructor parameter is assumed to be the same as the type of the field. In case of using super.field
,
the type is assumed to be the type of the parameter in super class constructor. Thus, there’s no need to declare it explicitly.
Exceptions
The rule doesn’t apply if the type of the parameter is different. This should be declared explicitly:
class Parent {
Iterable<String> children;
Parent(this.children);
}
class Person extends Parent {
Person(List<String> super.children); // Compliant, because the type is different
}