prefer_const_constructors_in_immutables
Constructors in '@immutable' classes should be declared as 'const'.
Description
#The analyzer produces this diagnostic when a non-const
constructor is found in a class that has the @immutable
annotation.
Example
#The following code produces this diagnostic because the constructor in C
isn't declared as const
even though C
has the @immutable
annotation:
dart
import 'package:meta/meta.dart';
@immutable
class C {
final f;
C(this.f);
}
Common fixes
#If the class really is intended to be immutable, then add the const
modifier to the constructor:
dart
import 'package:meta/meta.dart';
@immutable
class C {
final f;
const C(this.f);
}
If the class is mutable, then remove the @immutable
annotation:
dart
class C {
final f;
C(this.f);
}
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.