1

I want to check if the specific number exists in database so it will fetch the password corresponding to the number or else it will return number does not exist in database in 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");
  }
});

After this I'm getting the error Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on null

2
  • for What specific number you want to check existence of? Commented Apr 1, 2020 at 8:56
  • The number above the first unique key which starts with 9971*******. I've to fetch one value at a time. Commented Apr 1, 2020 at 10:01

2 Answers 2

4

Try the following:

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"]);
              });
          });
       }
   });

First add a reference to the newUsers and using the query orderByKey().equalTo(numberController.text) and then by doing snapshot.value.isNotEmpty it will check if key exists in the database, if it does retrieve the password.

Sign up to request clarification or add additional context in comments.

17 Comments

I'm getting this error ** Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on null.**
That error means you called the method on a null value - so check if your snapshot.value is returning null
your code is different than what I provided.. what is newUsers? There is no child("NewUsers") node in your database
Actually the number whose existence I'm looking for is the child of NewUsers.
before if(snapshot.value.isNotEmpty){ check if(snapshot.value !=null)
|
0

Try this:

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");
     }
 });

2 Comments

I'm getting error on if condition of snapshot, it says the operand of && operation must be assigned to bool.
Even I cannot directly pass the reference in instance.reference(" ").it says 0 positional argument expected and found one so I tried by providing reference in its child, but didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.