使用自定义更改 CheckBox 值 class 属性

Change CheckBox value with custom class property

这是我第一次在这里发帖,如果我做错了,我深表歉意。

我用布尔字段自定义了 class 来控制 CheckBox state/value:

class CustomModel {
static const TITLE_KEY = 'title';

  String id;
  String title;
  bool isChecked = false;

  void setIsChecked(bool isChecked) {
    this.isChecked = isChecked;
  }

  bool getIsChecked() {
    return isChecked;
  }

  CustomModel(Map<String, dynamic> ex) {
    title = ex[TITLE_KEY];
  }
}

在我的 CheckBox 小部件中:

return new ExpansionTile(
                title: const Text('Title'),
                backgroundColor: Colors.grey[100],
                children: snapshot.data.documents
                    .map((DocumentSnapshot document) {
                  CustomModel ex = new CustomModel(document.data);
                  return new ListTile(
                    title: new Text(ex.title),
                    trailing: new Checkbox(
                        value: ex.getIsChecked(),
                        onChanged: (bool value) {
                          setState(() {                             
                            ex.setIsChecked(value);
                          });
                        }),
                  );
                }).toList());
          }),

单击时 CheckBox 没有改变(有点闪烁)。日志显示 CheckBox 是 "false",然后是 "true",但是如果我再次单击同一个 CheckBox,日志显示 "false" 然后是 "true",所以它不会似乎正在更新该字段。

当我在 Java 中执行此操作时,我不必将值设置为 "isChecked",但如果我不这样做,Flutter 会抛出错误。

I/flutter ( 6408): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

I/flutter (6408): 构建 StreamBuilder(dirty, state: I/flutter(6408):_StreamBuilderBaseState>#0512c): I/flutter(6408):'package:flutter/src/material/checkbox.dart':断言失败:第 63 行:'三态 ||值!= I/flutter ( 6408): null': 不正确。

提前致谢!

我假设 return new ExpansionTile(..); 在你的 StatefulWidget class 的 build() 方法中。因此,当您调用 setState() 时,它将 re-build 个小部件,这意味着再次调用 return new ExpansionTile(..);。由于您的 CustomModel ex = new CustomModel(document.data); 也将再次实例化,并且此时 isChecked 将为假。所以解决方案是将 firestore 中的所有构建 CustomModel 列表放入 initState()

就是这个思路,(可能有一些语法错误,因为我直接写在这里)。

class MyWidget extends StatefulWidget {
   State createState() => new MyWidgetState();  } 

class MyWidgetState extends State<MyWidget> {
  List<CustomModel> customModels = []; 
  @override 
  initState() {
     //use listener to fetch data  
     Firestore.instance.collection("name").snapshots.listen((snapshot) {
        for (var doc in snapshot.documents) {
            CustomModel ex = new CustomModel(fieldname: doc['fieldname]);
            customModels.add(ex); 
        }

       }
    }

   @override
   Widget build(BuildContext) {
     return new ExpansionTile(
                title: const Text('Title'),
                backgroundColor: Colors.grey[100],
                children: customModels.map((CustomModel ex) {
                  return new ListTile(
                    title: new Text(ex.title),
                    trailing: new Checkbox(
                        value: ex.getIsChecked(),
                        onChanged: (bool value) {
                          setState(() {                             
                            ex.setIsChecked(value);
                          });
                        }),
                  );
                }).toList());
          }),
     }    

}