Skip to main content

non_redirecting_generative_constructor_with_primary

Details about the 'non_redirecting_generative_constructor_with_primary' diagnostic produced by the Dart analyzer.

Classes with primary constructors can't have non-redirecting generative constructors.

Description

#

The analyzer produces this diagnostic when a class that declares a primary constructor also has a non-redirecting generative constructor.

Example

#

The following code produces this diagnostic because the class C declares both a primary constructor and a non-redirecting generative constructor:

dart
class C(final String f) {
  C.c(Object p) : f = p.toString();
}

Common fixes

#

If the generative constructor can redirect to the primary constructor, then add the redirection:

dart
class C(final String f) {
  C.c(Object p) : this(p.toString());
}

If the generative constructor can be a factory constructor that uses the primary constructor to create the instance, then make it a factory constructor:

dart
class C(final String f) {
  factory C.c(Object p) => C(p.toString());
}

Otherwise, remove the generative constructor or convert the primary constructor into a generative secondary constructor.