const_ instance_ field
Only static fields can be declared as const.
Description
#The analyzer produces this diagnostic when an instance field is marked as being const.
Example
#
The following code produces this diagnostic because
f
is an instance
field:
class C {
const int f = 3;
}
Common fixes
#
If the field needs to be an instance field, then remove the keyword
const
, or replace it with
final
:
class C {
final int f = 3;
}
If the field really should be a const field, then make it a static field:
class C {
static const int f = 3;
}
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.