为什么这些切换在 ListBuilder 中同时工作?

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

将从模型中检索列表的数据。我在此列表生成器中有切换按钮列表 (10)。在提供共享首选项代码之前,所有切换都工作正常。但是在共享首选项之后,所有的切换同时进行但它保存了状态。这意味着共享首选项工作正常,但问题是,所有切换(所有 10 个切换)同时切换。

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

Widget build(BuildContext context) {
return Scaffold(
ListView.builder(
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>[
                                                    AnimatedPositioned(
                                                      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(),
                                                          ),
                                                        ),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              )

你是说下面的代码总是return“真”值?

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

可能的原因:

  1. this.preferences 为空。
  2. this.preferences?.getBool("toggleTask") 为空。

对于第 2 点,this.preferences?.getBool("toggleTask") 应该是 return Future<bool> ,您必须在呈现逻辑之前使用 await 或其他方式获取值。