Home > Mobile >  Switch not working when bool is in list - flutter
Switch not working when bool is in list - flutter

Time:08-18

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

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!

CodePudding user response:

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;
                  });
                })
        ],
      ),
    );
  }
}
  • Related