primary_ constructor_ body_ without_ declaration
Details about the 'primary_constructor_body_without_declaration' diagnostic produced by the Dart analyzer.
A primary constructor body requires a primary constructor declaration.
Description
#The analyzer produces this diagnostic when a class contains a primary constructor body but doesn't declare a primary constructor.
Example
#
The following code produces this diagnostic because the class C contains
a primary constructor body but doesn't declare a primary constructor:
class C {
this : assert(x > 0);
}
Common fixes
#If the constructor is intended to be a primary constructor, then add parameter declarations to the class header:
class C(int x) {
this : assert(x > 0);
}
If the constructor is intended to be a secondary constructor, then replace
the this with either the class name or new and declare the parameters in
the constructor declaration:
class C {
C(int x) : assert(x > 0);
}
If the constructor isn't needed, then remove the body:
class C {}
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.