Skip to content Skip to sidebar Skip to footer

The Function Setstate Is Not Defined. Flutter

I'm trying to change the colour of my checkbox(actually custom checkbox) for this I have written the code below , while I press the icon the onPressed() method is called but the co

Solution 1:

You must use a StatefulWidget.

A widget that has mutable state.

State is information that (1) can be read synchronously when the widget is built and (2) might change during the lifetime of the widget. It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes, using State.setState. For example

class MyDemo extends StatefulWidget {
  @override
  _MyDemoState createState() => _MyDemoState();
}

class _MyDemoState extends State<MyDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: IconButton(icon: null, onPressed: (){
        setState(() {
          //Do your stuff
        });
      }),
    );
  }
// every method which changes state should exist within class only
  Widget _check(){
    return IconButton(icon: null, onPressed: (){
      setState(() {

      });
    });
  }

}

Solution 2:

You must have a stateful widget to use setState. setState is used to change the state of the widget. You can go through Stateless & Stateful Widget documentation to read in-deapth about this.

Post a Comment for "The Function Setstate Is Not Defined. Flutter"