Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Also, why is it written widget.value?

I have tried using static instead of final doesn't work

class BottomCard extends StatefulWidget {

String title;

int value;
@override
_BottomCardState createState() => _BottomCardState(); }

class _BottomCardState extends State<BottomCard> {..... 


....<Widget>[  
        FloatingActionButton(
          elevation: 0,
          child: Icon(FontAwesomeIcons.plus),
          onPressed: () {
            setState(() {
              widget.value++;
            });
          },
          backgroundColor: Color(0xFF47535E),
        ),
like image 344
chaitanya Harde Avatar asked Sep 13 '19 18:09

chaitanya Harde


People also ask

Is the @immutable class final or not?

This class (or a class which this class inherits from) is marked as ' @immutable ', but one or more of its instance fields are not final: MusicHome.songs. new SnackBar (content: Text ("Play your first song.")));

Why stateless widgets are immutable in flutter?

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. 2.

When to initialize instance variables/fields declared final?

Instance variables/fields declared final must be initialized before a class’ constructor body starts. As stated above, one way such instance fields are initialized is as parameters in the class’ constructor.

What does it mean if a widget is immutable?

So immutable is just a warning. All fields in a class extending StatelessWidget should be final. From the documentations. StatelessWidget class. A widget that does not require mutable state.


Video Answer


3 Answers

If you don't need to get variables (title & value) from outside your widget, you can declare them in the _BottomCardState class and use them as you want, for example:

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 to:

  1. Mark them as final, or
  2. Get them from constructor

To access their values in _BottomCardStateState, you can access them using widget._value. They are final so you can't modify them. For example:

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
          });
        },
      ),
    );
  }
}
like image 81
Fatima Hossny Avatar answered Oct 31 '22 03:10

Fatima Hossny


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
          });
        },
      ),
    );
  }
}
like image 36
CopsOnRoad Avatar answered Oct 31 '22 04:10

CopsOnRoad


Just added this line top of the error class.

//ignore: must_be_immutable
like image 40
Mostafijur Rahman Avatar answered Oct 31 '22 02:10

Mostafijur Rahman