简体   繁体   English

FireBase getUid 在返回值后返回为 Null

[英]FireBase getUid Coming Back As Null After Returning a Value

I'm trying to create a child using the user's ID.我正在尝试使用用户 ID 创建一个孩子。 I found the ID when I created a user here and got the userID in the logcat:我在这里创建用户时找到了ID,并在logcat中获取了userID:

     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.检查了 logcat,看到了 userID 变量。 Checked the auth console, and got "authentication complete:true //id:userid".检查身份验证控制台,并得到“身份验证完成:真//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".我再次检查了我的 logcat,我得到了“身份验证完成:真//id:userid”“idcheck:null”。

Things I've tried:我尝试过的事情:

  • Changing the child to uname (another String variable).将孩子更改为 uname(另一个字符串变量)。 Data was posted into real time database as intended数据按预期发布到实时数据库中
  • Putting "" "" around userID.将 "" "" 放在 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将所有字符串和底部代码放在 onComplete 方法中

I had it working before but not exactly sure how, so I know it's possible.我以前让它工作但不确定如何工作,所以我知道这是可能的。 TIA! 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);
   

It looks like you're trying to access the userID outside of the onComplete callback, which isn't going to work.看起来您正在尝试在onComplete回调之外访问userID ,这是行不通的。

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.这样做的原因是身份验证是异步发生的,并且在您的reference.child(userID).setValue(newUser)运行时, userID = newUserID.getUid()尚未运行。 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.任何需要 UID 的代码都需要在用户登录完成时调用的onComplete内部,或者从那里调用。

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:另见:

So I made my variables global.所以我把我的变量设为全局变量。 Slight hiccup.轻微打嗝。 Edits are in the updated post.编辑在更新的帖子中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM