Dart has both named and unnamed constructors.
Named constructors are declared with a name, pretty much like methods, and can be invoked combinining the name of the enclosing type and the name
of the constructor (e.g. MyClass.myNamedConstructor()
).
Unnamed constructors can either be declared without a name, or with the special name new
. They can either be invoked using the name of
the enclosing type only (e.g. MyClass()
), or using the .new
keyword (e.g. new MyClass()
).
The .new
declaration and the name-less declaration are equivalent: they are two altenate syntaxes with the same meaning, and they are
mutually exclusive. Declaring both in the same class will result in a compilation error:
class AClass {
AClass() {}
AClass.new() {} // Error: The unnamed constructor is already defined
}
On the invocation end, invoking unnamed constructors with the .new
keyword is redundant and should be avoided, to reduce unnecessary
verbosity, improve code readability, and promote consistency.