prefer_ const_ constructors
Details about the 'prefer_const_constructors' diagnostic produced by the Dart analyzer.
Use 'const' with the constructor to improve performance.
Description
#
The analyzer produces this diagnostic when an invocation of a
const constructor isn't either preceded by
const or in a
constant contextConstant contextA region of code where the const keyword is implied and
everything within that region must be a constant.
Learn more.
Example
#
The following code produces this diagnostic because the
invocation of the
const constructor is neither prefixed by
const nor in a
constant contextConstant contextA region of code where the const keyword is implied and
everything within that region must be a constant.
Learn more:
class C {
const C();
}
C c = C();
Common fixes
#If the context can be made a constant contextConstant contextA region of code where the const keyword is implied and everything within that region must be a constant. Learn more, then do so:
class C {
const C();
}
const C c = C();
If the context can't be made a
constant contextConstant contextA region of code where the const keyword is implied and
everything within that region must be a constant.
Learn more, then add
const
before the constructor invocation:
class C {
const C();
}
C c = const C();
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.