【问题标题】:Flutter radio button on change is not updating FirebaseFlutter 更改单选按钮未更新 Firebase
【发布时间】: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


    【解决方案1】:

    我发现了问题。我将小部件的值用于接收 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);  
    
                              }
    

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 2021-08-05
      • 2021-01-02
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多