【发布时间】:2020-05-05 04:07:19
【问题描述】:
我是 Flutter 的新手,我正在尝试进行 CRUD,但遇到了问题。在我的更新屏幕上,我可以将表单上的所有字段发送到 Firebase Firestore,但无线电字段除外。这个问题只发生在更新中,因为在创建时收音机运行良好。我在更新中看不到哪里出错了,您能帮帮我吗?
下面的代码是我的课
class _DetailFormState extends State<DetailForm> {
final DataRepository repository = DataRepository();
final _formKey = GlobalKey<FormBuilderState>();
String sex;
@override
void initState() {
sex = widget.user.sex;
super.initState();
}
这些是我的单选按钮:
ListTile(
title: Align(
child: new Text("Female"),
alignment: Alignment(-1.2, 0),
),
leading: Radio(
value: "female",
groupValue: widget.user.sex,
onChanged: (String value) {
setState(() {
widget.user.sex = value;
});
},
),
),
ListTile(
title: Align(
child: new Text("Male"),
alignment: Alignment(-1.2, 0),
),
leading: Radio(
value: "male",
groupValue: widget.user.sex,
onChanged: (String value) {
setState(() {
widget.user.sex = value;
});
},
),
),
这是我调用 Firebase 存储库来保存数据的地方。正如我之前所说,我可以保存除收音机以外的任何字段。这是我的问题。不会出现错误消息。 Firebase 中的数据根本没有更新
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
color: Colors.blue.shade600,
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
"Cancel",
style: TextStyle(color: Colors.white, fontSize: 12.0),
)),
MaterialButton(
color: Colors.blue.shade600,
onPressed: () async {
Navigator.of(context).pop();
widget.user.sex = sex;
repository.updateUser(widget.user);
},
child: Text(
"Update",
style: TextStyle(color: Colors.white, fontSize: 12.0),
)),
],
),
编辑 1
updateUser(User user) async {
await collection
.document(user.reference.documentID)
.updateData(user.toJson());
}
【问题讨论】:
标签: firebase flutter dart radio-button