prefer_ const_ constructors
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 context.
Example
#
The following code produces this diagnostic because the invocation of the
const
constructor is neither prefixed by
const
nor in a
constant context:
class C {
const C();
}
C c = C();
Common fixes
#If the context can be made a constant context, then do so:
class C {
const C();
}
const C c = C();
If the context can't be made a
constant context, then add
const
before the constructor invocation:
class C {
const C();
}
C c = const C();
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.