Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch not working when bool is in list – flutter

quick question, let me know if I need to link stack etc.

I have a switch widget which works just fine. However, if I put the boolean, that I alter with my setstate, in a List I get the error message "type ‘bool’ is not a subtype of type ‘List’ of ‘function result’ ". Not sure why this is not working since I’m doing the exact same as I am in my toggleButtons and it’s working there.

code looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Switch(value: isChosen[0], onChanged: (value) {
                            setState(() {
                              isChosen[0] = value;
                            });
                          })

and list just like this

List<bool> isChosen = [true, false, false]

Thankful for any help on this rather small issue!

>Solution :

You can follow this snippet.

class GA23 extends StatefulWidget {
  GA23({Key? key}) : super(key: key);

  @override
  State<GA23> createState() => _GA23State();
}

class _GA23State extends State<GA23> {
  List<bool> isChosen = [true, false, false];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          for (int i = 0; i < isChosen.length; i++)
            Switch(
                value: isChosen[i],
                onChanged: (value) {
                  setState(() {
                    isChosen[i] = value;
                  });
                })
        ],
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading