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:
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:
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:
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.
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.