更改时的 Flutter 单选按钮不会更新 Firebase
Flutter radio button on change is not updating Firebase
我是 Flutter 的新手,我正在尝试执行 CRUD,但遇到了问题。在我的更新屏幕上,我可以将表单上的所有字段发送到 Firebase Firestore,除了单选字段。此问题仅发生在更新中,因为在创建过程中收音机运行良好。我在更新中看不出哪里出错了,你能帮我吗?
下面的代码是我的class
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 存储库来保存数据的地方。正如我之前所说,我可以保存除 radio 之外的任何字段。这是我的问题。没有错误信息出现。 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 中已经存在的变量的值:
onPressed: () async {
Navigator.of(context).pop();
widget.user.sex = sex;
repository.updateUser(widget.user);
}
其实应该反其道而行之。 Firebase 中的变量接收小部件中的值:
onPressed: () async {
Navigator.of(context).pop();
sex = widget.user.sex;
repository.updateUser(widget.user);
}
我是 Flutter 的新手,我正在尝试执行 CRUD,但遇到了问题。在我的更新屏幕上,我可以将表单上的所有字段发送到 Firebase Firestore,除了单选字段。此问题仅发生在更新中,因为在创建过程中收音机运行良好。我在更新中看不出哪里出错了,你能帮我吗?
下面的代码是我的class
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 存储库来保存数据的地方。正如我之前所说,我可以保存除 radio 之外的任何字段。这是我的问题。没有错误信息出现。 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 中已经存在的变量的值:
onPressed: () async {
Navigator.of(context).pop();
widget.user.sex = sex;
repository.updateUser(widget.user);
}
其实应该反其道而行之。 Firebase 中的变量接收小部件中的值:
onPressed: () async {
Navigator.of(context).pop();
sex = widget.user.sex;
repository.updateUser(widget.user);
}