0

I'm trying to create a child using the user's ID. I found the ID when I created a user here and got the userID in the logcat:

     final String fullName = findViewByid(R.id.fullName);
     . 
     .    // Other strings declared here
     .
     public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser newUserID = mAuth.getCurrentUser();
                        userID = newUserID.getUid();
                        Log.w(TAG, "AuthenticationComplete:true //id:" + userID);
                        

Checked the logcat, seen the userID variable. Checked the auth console, and got "authentication complete:true //id:userid".

This is where the problem comes in.

     public void onComplete(@NonNull Task<AuthResult> task) {
     .
     .
     .
     });
     Log.w(TAG, "AuthenticationComplete:true //id:" + userID);
     BasicUser newUser = new BasicUser(fullName, uname, userEmail, password, confirmPassword, city, newState, zip, type);
     Log.w(TAG, "idcheck:" + userID);
     **reference.child(userID).setValue(newUser);**

I checked my logcat again, I got "authentication complete:true //id:userid" "idcheck: null".

Things I've tried:

  • Changing the child to uname (another String variable). Data was posted into real time database as intended
  • Putting "" "" around userID. Not sure what I thought it was going to do but it was worth a shot
  • Placing all strings and bottom code in onComplete method

I had it working before but not exactly sure how, so I know it's possible. TIA!

UPDATE:

I made the variables global and not final. It's working as expected now.

public class(){
String fullName;
public void signUp(){
     public void onComplete(@NonNull Task<AuthResult> task) {
     
     });
     Log.w(TAG, "AuthenticationComplete:true //id:" + userID);
     BasicUser newUser = new BasicUser(fullName, uname, userEmail, password, confirmPassword, city, newState, zip, type);
     Log.w(TAG, "idcheck:" + userID);
     reference.child(userID).setValue(newUser);
   
Krys
  • 41
  • 8

2 Answers2

1

It looks like you're trying to access the userID outside of the onComplete callback, which isn't going to work.

The reason for this is that the authentication happens asynchronously, and by the time that your reference.child(userID).setValue(newUser) runs, the userID = newUserID.getUid() hasn't run yet. If you run the code in a debugger and place breakpoints on these lines, or add some logging to them, you can most easily see this.

Any code that needs the UID needs to be inside the onComplete that gets called when the user sign in is complete, or be called from there.

So:

 public void onComplete(@NonNull Task<AuthResult> task) {
     if (task.isSuccessful()) {
         FirebaseUser newUserID = mAuth.getCurrentUser();
         userID = newUserID.getUid();

         BasicUser newUser = new BasicUser(fullName, uname, userEmail, password, confirmPassword, city, newState, zip, type);
         reference.child(userID).setValue(newUser);

         ...
});

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

So I made my variables global. Slight hiccup. Edits are in the updated post.

Krys
  • 41
  • 8