'Generated boolean list cannot be changed its value

I need to create an authentication page. It has locations. I have gathered the info from api with future and i assigned them with generated bool list but i cannot change their values. Here is my code examples...

ListView.builder(
   padding: EdgeInsets.fromLTRB(0,12,0,0),
   shrinkWrap: true,
   primary: false,
   itemCount: snapshot.data!.locationList!.length,
   itemBuilder: 
      (context, index) {
         final locations = snapshot.data!.locationList!;
         List<bool>? x = List<bool>.generate(locations.length, (index) => false);
         debugPrint(x[0].toString()); //false
         debugPrint(locations[0].id.toString()); //1
         return Padding(
                   padding: const EdgeInsets.fromLTRB(12,6,12,6),
                   child: Row(
                             mainAxisAlignment: MainAxisAlignment.spaceBetween,
                             children: <Widget>[
                             Text('${locations[index].name}', style: TextStyle(fontSize: 20),),
                             FlutterSwitch(
                                 width: width*.2,
                                 height: 50.0,
                                 activeColor: col.mostDarkPurpleAccent,
                                 toggleSize: width*.06,
                                 value: LoginApi.distinctLocation.contains(index+1) ? !x[index] : x[index],
                                 borderRadius: 30.0,
                                 padding: 8.0,
                                 onToggle: (bool val) {
                                          debugPrint(val.toString()); //false
                                          setState(() {
                                            debugPrint('setState başı: '+x[index].toString()); //false
                                            if (LoginApi.distinctLocation.contains(index+1)) {
                                              debugPrint('if başı: '+x[index].toString()); //false
                                              x[index] = !val;
                                              debugPrint('if sonu: '+x[index].toString()); //true
                                            } else {
                                              debugPrint('else başı: '+x[index].toString()); //not in
                                              x[index] = !val;
                                              debugPrint('else sonu: '+x[index].toString()); //not in
                                            }
                                            debugPrint('setState sonu: '+x[index].toString()); //true
                                          });
                             },
                       ),
                   ],
               ),
         );
    },
),

Here are codes, but i cannot toggle it. What is the problem? Could you help me, pls?



Solution 1:[1]

If you are setting a value to x[index] in your onToggle: (bool val){} and call setState() then your value is already changed and affects to your view. So you dont need to do that: value:LoginApi.distinctLocation.contains(index+1) ? !x[index] : x[index], use instead only: value: x[index],

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maikzen