【问题标题】:how to check if data exists in a document firebase flutter如何检查文档中是否存在数据firebase颤动
【发布时间】:2021-05-24 15:25:05
【问题描述】:

当用户按下此按钮时,我正在尝试检查用户输入数据是否存在于 Firebase 中。

如果存在就去注册

这是按钮


                          onPressed: () {
                            setState(() {
                              if (studentId == null){
                                 //error message 
                              }else {
                                _firebase
                                    .collection("university_ids")
                                    .doc(studentId)
                                    .get()
                                    .then((doc) {
                                  if (doc.exists) {
                                    setState(() {
                                      isExists = true;
                                    });
                                  } else {
                                    setState(() {
                                      isExists = false;
                                    });
                                  }
                                });
                                if (isExists) {
                                  Navigator.pushNamed(
                                      context, 'sign_up_screen');
                                } 

这是文本字段

                            onChange: (value) {
                              setState(() {
                                studentId= value;
                              });
                            },
                        

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    您似乎根本不需要setState 电话。

    只需对onPressed 回调使用async 函数并使用await 异步获取数据。

    onPressed: () async {
      if (studentId == null) { //error message }
      else {
         var doc = await _firebase.collection("university_ids").doc(studentId).get();
                                  
         if (doc.exists) Navigator.pushNamed(context, 'sign_up_screen');
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2022-01-21
      • 2019-01-09
      • 2021-06-15
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多