Home > Blockchain >  Android Studio dataSnapshot exists but nothing is getting printed Firebase
Android Studio dataSnapshot exists but nothing is getting printed Firebase

Time:12-31

I'm confused as to why this is not working and I have tried debugging it a lot and I have no idea why is this not working

FirebaseDatabase.getInstance().getReference().child("users").child("name").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Log.d("dataSnapshot", "exists");
                for (DataSnapshot snapshot1 : dataSnapshot.getChildren()) {
                    Log.d("inside snapshot", "snapshot not null");
                    String name = snapshot1.getValue().toString();
                    Log.d("user created", "snapshot not null");
                    try {
                        Log.d("name", name);
                        Log.d("decrypt", aes.Decrypt(name));
                    } catch (UnsupportedEncodingException e) {
                        Toast.makeText(EnterName.this, "Exception", Toast.LENGTH_LONG);
                        e.printStackTrace();
                    }
                    Toast.makeText(EnterName.this, "logged", Toast.LENGTH_LONG);
                }
            }
            else
            {
                Toast.makeText(EnterName.this,"doesnt exist", Toast.LENGTH_SHORT);
            }
        }

I'm getting the log that dataSnapshot exists but nothing after that. Firebase data is Firebase Data

Anyone has any idea? Thank you.

CodePudding user response:

You're listening to just the users/name property, which means you get a snapshot with the key name and a string value "check". A string value doesn't have any children, so dataSnapshot.getChildren() returns an empty list.

To print the value of your snapshot:

FirebaseDatabase.getInstance().getReference().child("users").child("name").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull @NotNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            Log.d("dataSnapshot", dataSnapshot.getValue(String.class));
        }
        ...
  • Related