0

I'm facing an problem with my app. When the user registers in the app, automatticaly it is created a "profile" of the user in a database, where the Uid is the child:

Firebase User Data

After the Login in the app is the app calls the activity2. On start it is suppost to load the user information into a class:

{
    tUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            if (snapshot.child(fauth.getUid()).child("uBirthday").exists()) {
            }
            else {

//userData is created when activity2 is called

              userData= (snapshot.child(fauth.getUid()).getValue(Class_UserData.class));
              
              Log.i("MainActivity inside", "User: " + userData.getUserName() + " Email: " + userData.getUserEmail());    
            }
        }
        @Override
        public void onCancelled (@NonNull DatabaseError error){
            return;
        }
    });

Below Class_UserData:


    String uName;
    String uEmail;
    String UserBirthday;
    String UserGender;
    int UserPhoto;


    public Class_UserData(String userName, String userEmail) {
        uName = userName;
        uEmail = userEmail;
    }

    public Class_UserData(String userName, String userEmail, String userBirthday, String userGender) {
        uName = userName;
        uEmail = userEmail;
        UserBirthday = userBirthday;
        UserGender = userGender;
    }

    public Class_UserData() {
    }

    public void setUserName(String userName) {
        uName = userName;
    }

    public void setUserEmail(String userEmail) {
        uEmail = userEmail;
    }

    public void setUserBirthday(String userBirthday) {
        UserBirthday = userBirthday;
    }

    public void setUserGender(String userGender) {
        UserGender = userGender;
    }

    public String getUserName() {
        return uName;
    }

    public String getUserEmail() {
        return uEmail;
    }

    public String getUserBirthday() {
        return UserBirthday;
    }

    public String getUserGender() {
        return UserGender;
    }
}

When inside the onDataChange the class is working. OutSide the class it returns null. Any ideas how to solve this?

punzipt
  • 1
  • 2
  • so after login you want to lead its data right.? simply get user using userId from node and then save it to preferences. – Atif AbbAsi Nov 28 '20 at 14:58
  • Yes. Exactly. The addValueEventListener is inside a function that is called on OnStart. Just on more thing, on the picture there is a field 'Dt Nasciment' that field does not exists anymore. – punzipt Nov 28 '20 at 15:02
  • 1
    Data is loaded from Firebase asynchronously, and your main code continues while this is going on. Any code that needs data from the database, needs to be inside `onDataChange` or be called from there. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Nov 28 '20 at 15:02
  • after successfully getting data from auth , pass user Id to next function and fetch record of user saved in databased. – Atif AbbAsi Nov 28 '20 at 15:04
  • here fireabase id is unique for each user you can addValueEventListener .child(userNode).orderById(firebaseAuthId) – Atif AbbAsi Nov 28 '20 at 15:06

0 Answers0