const_constructor_with_non_const_super
A constant constructor can't call a non-constant super constructor of '{0}'.
Description
#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();
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.