This class is marked as '@immutable', but one or more of its instance fields are not final: This class is marked as '@immutable', but one or more of its instance fields are not final: dart dart

This class is marked as '@immutable', but one or more of its instance fields are not final:


If you don't need to get variables (title & value) from out side class . you can declare them in _BottomCardState class and use them as you want.like this code .

class BottomCardState extends StatefulWidget {  @override  _BottomCardStateState createState() => _BottomCardStateState();}class _BottomCardStateState extends State<BottomCardState> {  int _value;  String title;  @override  void initState() {    super.initState();    _value = 0;    title = "any thing";  }  @override  Widget build(BuildContext context) {    return Scaffold(      floatingActionButton: FloatingActionButton(        elevation: 0,        child: Icon(FontAwesomeIcons.plus),        onPressed: () {          setState(() {            _value++; // increment value here          });        },      ),    );  }}

If you need to get variables (value & title) from other class.Then you need to1-Mark them as final . 2-Get them from constructorand to access their values in _BottomCardStateState you need to access them using widget._value.they are final you can't modify them.like this code here

class App extends StatelessWidget {  const App({Key key}) : super(key: key);  @override  Widget build(BuildContext context) {    return Container(      child: BottomCardState(2,"some thing"),    );  }}class BottomCardState extends StatefulWidget {  final int _value;  final String title;  BottomCardState(this._value,this.title)  @override  _BottomCardStateState createState() => _BottomCardStateState();}class _BottomCardStateState extends State<BottomCardState> {  int value ;  @override  Widget build(BuildContext context) {    value = widget._value ;    return Scaffold(      floatingActionButton: FloatingActionButton(        elevation: 0,        child: Icon(FontAwesomeIcons.plus),        onPressed: () {          setState(() {          value++; // increment value here          });        },      ),    );  }}


If I am declaring the variables as final then then the values(variables) i want to change(on pressed) are in set state(){} so those variables can be changed What to do to prevent this?

Extend StatefulWidget.

Also why is it written widget.value?

This means you are accessing value in your StatefulWidget subclass and this value is actually defined in your State class.


Full code should look like:

class BottomCardState extends StatefulWidget {  @override  _BottomCardStateState createState() => _BottomCardStateState();}class _BottomCardStateState extends State<BottomCardState> {  int _value = 0; // set value here  @override  Widget build(BuildContext context) {    return Scaffold(      floatingActionButton: FloatingActionButton(        elevation: 0,        child: Icon(FontAwesomeIcons.plus),        onPressed: () {          setState(() {            _value++; // increment value here          });        },      ),    );  }}