const_with_non_const
The constructor being called isn't a const constructor.
Description
#The analyzer produces this diagnostic when the keyword const
is used to
invoke a constructor that isn't marked with const
.
Example
#The following code produces this diagnostic because the constructor in A
isn't a const constructor:
dart
class A {
A();
}
A f() => const A();
Common fixes
#If it's desirable and possible to make the class a constant class (by
making all of the fields of the class, including inherited fields, final),
then add the keyword const
to the constructor:
dart
class A {
const A();
}
A f() => const A();
Otherwise, remove the keyword const
:
dart
class A {
A();
}
A f() => A();
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。