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:
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:
class A {
const A();
}
A f() => const A();
Otherwise, remove the keyword const
:
class A {
A();
}
A f() => A();
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.