Why these toggles all working at the same time in ListBuilder?


The data for list will be retrieved from the model. I have list of toggle buttons(10) in this list builder. All the toggle worked fine before giving the shared preference code. But after the shared preference all the toggle switching at the same time but it saves the state. which means the shared preference working fine but the problem is, all toggle (all the 10 toggles) switching same time.

class _MyScreenState extends State<MyScreen> {
var data = Data();

Widget build(BuildContext context) {
return Scaffold(
itemCount:  data.pc.childClass.length,
itemBuilder: (context, i) {
return Container(
AnimatedContainer(duration: Duration(milliseconds: 1000),
                                                height: 35.0,
                                                width: 70.0,
                                                decoration: BoxDecoration(
                                                    borderRadius: BorderRadius.circular(20.0),
                                                    color: this.preferences?.getBool("toggleTask") ?? data.prnt[i].childClass[0].isOn ? Color(0xFF1F8BD0): Colors.grey[100]!.withOpacity(0.2)
                                                child: Stack(
                                                  children: <Widget>[
                                                      duration: Duration(milliseconds: 400),
                                                      curve: Curves.ease,
                                                      left: this.preferences?.getBool("toggleTask") ?? data.prnt[i].childClass[0].isOn ? 30.0 : 0.0,
                                                      right: this.preferences?.getBool("toggleTask") ?? data.prnt[i].childClass[0].isOn ? 0.0 : 30.0,
                                                      child: InkWell(
                                                        onTap: () {
                                                          setState(() {
                                                            data.prnt[i].childClass[0].isOn = ! data.prnt[i].childClass[0].isOn;
                                                            this.preferences?.setBool("toggleTask", data.prnt[i].childClass[0].isOn);
                                                        child: AnimatedSwitcher(
                                                          duration: Duration(milliseconds: 10),
                                                          transitionBuilder: (Widget child, Animation<double> animation) {
                                                            return ScaleTransition(child: child, scale: animation);
                                                          child: this.preferences?.getBool("toggleTask") ?? data.prnt[i].childClass[0].isOn ? Icon(Icons.circle, color: Colors.white, size: 35.0,
                                                            key: UniqueKey(),
                                                          ) : Icon(Icons.circle, color: Colors.white, size: 35.0,
                                                            key: UniqueKey(),


Do you mean the following code is always return "true" value?

this.preferences?.getBool("toggleTask") ?? data.prnt[i].childClass[0].isOn 

Possible reasons:

  1. this.preferences is null.
  2. this.preferences?.getBool("toggleTask") is null.

For point 2, this.preferences?.getBool("toggleTask") should be return Future<bool> , you have to use await or other way to get the value before you present you logic.

Answered By – Kin Cheng

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *