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:
dart
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
:
dart
class C {
final int f = 3;
}
If the field really should be a const field, then make it a static field:
dart
class C {
static const int f = 3;
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.