Constant constructor can't call a non-constant super constructorDRT-W1267
The analyzer produces this diagnostic when a constructor that is marked as
const
invokes a constructor from its superclass that isn't marked as
const
.
Example
The following code produces this diagnostic because the const
constructor
in B
invokes the constructor nonConst
from the class A
, and the
superclass constructor isn't a const
constructor:
class A {
const A();
A.nonConst();
}
class B extends A {
const B() : super.nonConst();
}
Common fixes
If it isn't essential to invoke the superclass constructor that is currently being invoked, then invoke a constant constructor from the superclass:
class A {
const A();
A.nonConst();
}
class B extends A {
const B() : super();
}
If it's essential that the current constructor be invoked and if you can
modify it, then add const
to the constructor in the superclass:
class A {
const A();
const A.nonConst();
}
class B extends A {
const B() : super.nonConst();
}
If it's essential that the current constructor be invoked and you can't
modify it, then remove const
from the constructor in the subclass:
class A {
const A();
A.nonConst();
}
class B extends A {
B() : super.nonConst();
}