'This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final)
Getting the below issue, and not able to solve it, Can anyone help me in solving this.
This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final). dart(must_be_immutable)

Thanks.
Solution 1:[1]
This issue occurs when all the class variables are not declared as final. You can either make them final or for ignoring this warning you can add the following comment above the class declaration:
//ignore: must_be_immutable
Solution 2:[2]
All fields in a class extending StatelessWidget should be final.
From the documentations.
StatelessWidget class. A widget that does not require mutable state.
https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html
Solution 3:[3]
Make sure that all your instances variable in class are marked as final and the object is also marked as final.
In class
class Items {
final String title;
final String author;
final String event;
final String img;
Items({this.title, this.author, this.event, this.img});
}
Inside widget that you in,
final Items item2 = new Items(
title: "Country",
author: "Balaji",
event: "4 Items",
img: "assets/panda.png",
);
Solution 4:[4]
There are some coding thing are prefix in flutter and one of them is that, Stateless widgets are immutable so we have define all variables with Final keyword in root class. If we would define all the variables as Final then this error would remove from given class.
To remove the error we have to define all the variables as Final for example
final String abc = 'hello';. To know more about this error solution you can read the Flutter official documentation here.
final, const and @immutable
What if we made these properties final?
class Product {
final String id;
final String name;
final Color color;
const Product({
this.id,
this.name,
this.color,
}) ;
}
As expected, adding final means that we can no longer assign values to id, name and color in the way we did before.
We're also able to add the const keyword here because the constructor is initializing the Product class with our final fields, making this a compile-time constant.
We can also add the @immutable annotation to our class which shows an error if any of the fields are not final:
@immutable
class Product {
String id;
final String name;
final Color color;
Product({
this.id,
this.name,
this.color,
});
}
Solution 5:[5]
Use That way
class Frog extends StatelessWidget {
const Frog({
Key key,
this.color = const Color(0xFF2DBD3A),
this.child,
}) : super(key: key);
final Color color;
final Widget child;
@override
Widget build(BuildContext context) {
return Container(color: color, child: child);
}
}
Solution 6:[6]
From flutter doc:
The following is a skeleton of a stateless widget subclass called GreenFrog. Normally, widgets have more constructor arguments, each of which corresponds to a final property.
Just like this
class IconContainer extends StatelessWidget {
final double size;
final Color color;
final IconData icon;
IconContainer(this.icon, {this.color=Colors.white, this.size=32});
Widget build(BuildContext context) {
//TODO...
}
Solution 7:[7]
Any an immutable class add after it "final" to next line of flutter code, this will solve your problem,
For example:-
class Register extends statelesswidget {
static const string idScreen = 'register';
final TextEditingController name = TextEditingController();
final TextEditingController email = TextEditingController();
}
Solution 8:[8]
Simply add this line top of the warning:
//ignore: must_be_immutable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
