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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
dartKnightRises
  • 815
  • 15
  • 26

2 Answers2

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I'm getting this error ** Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on null.** – dartKnightRises Apr 01 '20 at 09:59
  • That error means you called the method on a null value - so check if your ```snapshot.value``` is returning null – Amin Apr 01 '20 at 10:02
  • your code is different than what I provided.. what is `newUsers`? There is no `child("NewUsers")` node in your database – Peter Haddad Apr 01 '20 at 10:08
  • Actually the number whose existence I'm looking for is the child of NewUsers. – dartKnightRises Apr 01 '20 at 10:11
  • I represented the number with black "No" in the image. – dartKnightRises Apr 01 '20 at 10:13
  • is not `newUsers` above the "no" number? – Peter Haddad Apr 01 '20 at 10:14
  • Yes, newUsers is above the number. – dartKnightRises Apr 01 '20 at 10:18
  • And newUsers contains lot of numbers. So I want to check the existence of number provided by user and if the number exists in database it will fetch the corresponding password. – dartKnightRises Apr 01 '20 at 10:21
  • @PraveenGupta in you code you should do `dbRef.orderByKey().equalTo(numberController.text).once().then((DataSnapshot` – Peter Haddad Apr 01 '20 at 10:51
  • NoSuchMethodError: The method '[]' was called on null.Tried calling: []("Password") – dartKnightRises Apr 01 '20 at 11:16
  • E/flutter (28868): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'forEach' was called on null. E/flutter (28868): Receiver: null E/flutter (28868): Tried calling: forEach(Closure: (dynamic, dynamic) => Null) – dartKnightRises Apr 01 '20 at 11:26
  • Hey @PeterHaddad, this edited code worked for me. Thank You so much. It just need a small change you cannot directly provide reference in instance.reference("Here").So we need to add child of it and then provide the reference. – dartKnightRises Apr 01 '20 at 13:35
  • Hey @PeterHaddad , One more problem when the provided number does not exists in database ,it will not move to the else part instead it throws an error saying "The getter 'isNotEmpty' was called on null." – dartKnightRises Apr 01 '20 at 13:52
  • 1
    before `if(snapshot.value.isNotEmpty){` check `if(snapshot.value !=null)` – Peter Haddad Apr 01 '20 at 13:58
  • Hey @PeterHaddad, the above code fetch the password two times and one time it shows null and other time it fetch the correct one. How should I resolve this. – dartKnightRises Apr 03 '20 at 11:25
  • @PraveenGupta please ask a different question, Im sure u are getting this null first because the data is still not retrieved.. the API is asynchronous https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean, please ask a different question with more details and write the code where u are using the result of `once()` – Peter Haddad Apr 03 '20 at 11:56
  • @PeterHaddad I got the null value second times. Even I asked one different question separately. – dartKnightRises Apr 03 '20 at 15:44
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");
     }
 });
Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28
  • I'm getting error on if condition of snapshot, it says the operand of && operation must be assigned to bool. – dartKnightRises Apr 01 '20 at 13:29
  • 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. – dartKnightRises Apr 01 '20 at 13:32