primary_ constructor_ cannot_ redirect
Details about the 'primary_constructor_cannot_redirect' diagnostic produced by the Dart analyzer.
A primary constructor can't be a redirecting constructor.
Description
#The analyzer produces this diagnostic when a primary constructor redirects to another constructor.
Example
#
The following code produces this diagnostic because the primary
constructor has a redirect to the constructor C.named:
class C(int x) {
C.named() : this(0);
this : assert(x > 0), this.named();
}
Common fixes
#If the redirection isn't necessary, then remove it:
class C(int x) {
C.named() : this(0);
this : assert(x > 0);
}
If the redirection is necessary, then convert the primary constructor into a secondary constructor, and rework other constructors as necessary:
class C {
C.named();
C(int x) : this.named();
}
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.