在Flutter中从Firebase实时数据库中获取特定数据

时间:2020-04-01 08:35:31

标签: firebase flutter dart firebase-realtime-database

我想检查数据库中是否存在特定的号码,以便它将获取与该号码相对应的密码,否则它将返回Flutter中数据库中不存在的号码。

enter image description here

var dbRef = FirebaseDatabase.instance.reference().child("NewUsers");
dbRef.equalTo(numberController.text).once().then((DataSnapshot snapshot){
  if(snapshot.value.isNotEmpty){
    var dbRef = FirebaseDatabase.instance.reference();
    dbRef.child(numberController.text).once().then((DataSnapshot snapshot){
      snapshot.value.forEach((key,values) {
        print(values["Password"]);
      });
    });
  }
  else{
    print("pleasesignup first");
  }
});

此后,我得到错误Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on null

2 个答案:

答案 0 :(得分:3)

尝试以下操作:

var dbRef = FirebaseDatabase.instance.reference().child("newUsers");
dbRef.orderByKey().equalTo(numberController.text).once().then((DataSnapshot snapshot){
     if(snapshot.value.isNotEmpty){
         var ref = FirebaseDatabase.instance.reference("newUsers");
           ref.child(numberController.text).once().then((DataSnapshot snapshot){
                print(snapshot.value); 
                snapshot.value.forEach((key,values) {
                   print(values["Password"]);
              });
          });
       }
   });

首先添加对newUsers的引用,并使用查询orderByKey().equalTo(numberController.text),然后执行snapshot.value.isNotEmpty,它将检查数据库中是否存在密钥,如果确实检索到{{1 }}。

答案 1 :(得分:0)

尝试一下:

var dbRef = FirebaseDatabase.instance.reference().child("newUsers");
dbRef.orderByKey().equalTo(numberController.text).once().then((DataSnapshot snapshot){
     if(snapshot && snapshot.value){
         var ref = FirebaseDatabase.instance.reference("newUsers");
         ref.child(numberController.text).once().then((DataSnapshot snapshot){
           print(snapshot.value); 
           snapshot.value.forEach((key,values) {
             print(values["Password"]);
          });
        });
     }
     else{
       print("No Record Found");
     }
 });