Reputation: 905
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.
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
Upvotes: 1
Views: 4252
Reputation: 80952
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
.
Upvotes: 4
Reputation: 1366
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");
}
});
Upvotes: 0