skip to Main Content

I’ll put the code briefly to show how everything is meant to flow from the Stateful widget.

I basically want the checkbox on my card to change state when the user clicks on it.

Error message: The function ‘setState’ isn’t defined.

I did my research and I know it only works under a stateful widget. My widget tree is under a StatefulWidget so it should work but isn’t.

class MyAppState extends StatefulWidget{

       Scaffold{
          //calls CardWidget to build individual cards for my list
       }       
}

     Widget CardWidget{

          Return Inkwell {
              Container{
                   
                        child: Checkbox(
                             value: item.checkBox,
                             onChanged: (bool? value) {
                             item.checkBox = !value!;

                             setState(() {
                             item.checkBox;
                             });
                           },
                       ),

3

Answers


  1. This will be the widget structure

    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      var item; // the place you like
    
      Widget CardWidget() {
        return InkWell(
          onTap: () {},
          child: Container(
            child: Checkbox(
              value: item.checkBox,
              onChanged: (bool? value) {
                item.checkBox = !value!;
    
                setState(() {
                  item.checkBox;
                });
              },
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold();
      }
    }
    

    More about StatefulWidget

    Login or Signup to reply.
  2. You should put the CardWidget inside a class that extends StatefulWidget:

    class CardWidget extends StatefulWidget {
      final Item item;
    
      CardWidget(this.item);
    
      @override
      _CardWidgetState createState() => _CardWidgetState();
    }
    
    class _CardWidgetState extends State<CardWidget> {
      @override
      Widget build(BuildContext context) {
        return InkWell(
          onTap: () {
            setState(() {
              widget.item.checkBox = !widget.item.checkBox;
            });
          },
          child: Container(
            child: Checkbox(
              value: widget.item.checkBox,
              onChanged: (bool? value) {
                setState(() {
                  widget.item.checkBox = !value!;
                });
              },
            ),
          ),
        );
      }
    }
    

    now with _CardWidgetState you can use setState.

    happy coding….

    Login or Signup to reply.
  3. CarWidget() has to be written liket this:

    class CardWidget extends StatefulWidget {
      const CardWidget({Key? key}) : super(key: key);
    
      @override
      _CardWidgetState createState() => _CardWidgetState();
    }
    
    class _CardWidgetState extends State<CardWidget> {
      @override
      Widget build(BuildContext context) {
        return InkWell(child: 
          Container(child:
           Checkbox(
           value: item.checkBox,
            onChanged: (bool? value) {
             item.checkBox = !value!;
    
             setState(() {
                item.checkBox;
             });
           },
           ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search