Skip to main content

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:

dart
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:

dart
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:

dart
class C {
  C(int x) : assert(x > 0);
}

If the constructor isn't needed, then remove the body:

dart
class C {}