must_be_immutable
This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final: {0}
Description
#The analyzer produces this diagnostic when an immutable class defines one or more instance fields that aren't final. A class is immutable if it's marked as being immutable using the annotation immutable
or if it's a subclass of an immutable class.
Example
#The following code produces this diagnostic because the field x
isn't final:
import 'package:meta/meta.dart';
@immutable
class C {
int x;
C(this.x);
}
Common fixes
#If instances of the class should be immutable, then add the keyword final
to all non-final field declarations:
import 'package:meta/meta.dart';
@immutable
class C {
final int x;
C(this.x);
}
If the instances of the class should be mutable, then remove the annotation, or choose a different superclass if the annotation is inherited:
class C {
int x;
C(this.x);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.