When my application starts, I check the current user\'s details in my Firebase database (I\'m storing it\'s uid for that).
I\'m attaching addListenerForSingleValueEv
My guess is user/:id
does not exist. Double-check your error output:
DatabaseReference newUser = FirebaseDatabase.getInstance()
.getReference("users")
.child(uid)
.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
Log.i(TAG, snapshot.val());
} else {
Log.e(TAG, "Not found: " + uid);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.toString();
}
});
I've been having the same problem for days and I finally fixed it.
Looks like my Api key was changed! And that's what was preventing addListenerForSingleValueEvent()
from returning!
Go to Firebase Console, open your project, click the settings icon and go to Project settings. There you'll see Web API Key
. Take that and copy it to your google-services.json
file in your project's directory. Here's a snippet from that file:
...
],
"api_key": [
{
"current_key": "<COPY Web Api Key HERE>"
}
],
...
That's it. After replacing the value of "current_key" with the key from Firebase, it returned to work (previously it had some other key I think I generated at Google Console and it worked for months. Just a few days ago it stopped working)
The recommendation I've seen for this is to call keepSynced(true)
on the Query
object.
Please try like this. If you can't solver by this, send share me source code for this part. ...
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final HashMap<String, Object> dataMap = (HashMap<String, Object>) dataSnapshot.getValue();
for (String key : dataMap.keySet()){
Object data = dataMap.get(key);
try{
HashMap<String, Object> moneyData = (HashMap<String, Object>) data;
Money money = new Money(("", "");
}catch (ClassCastException cce){
try{
name = String.valueOf(dataMap.get("username"));
email = String.valueOf(dataMap.get("useremail"));
}catch (ClassCastException cce2){
}
}
}
// do something
}else {
// when empty data
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});