英文:
FireBase getUid Coming Back As Null After Returning a Value
问题
我正在尝试使用用户的ID创建一个子用户。我在这里创建用户时找到了ID,并在logcat中获得了userID:
final String fullName = findViewByid(R.id.fullName);
// 其他字符串在这里声明
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser newUserID = mAuth.getCurrentUser();
userID = newUserID.getUid();
Log.w(TAG, "AuthenticationComplete:true //id:" + userID);
}
}
检查了logcat,看到了userID变量。检查了auth控制台,得到了"authentication complete:true //id:userid"。
问题出现在这里:
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);
}
我再次检查了logcat,得到了"authentication complete:true //id:userid"和"idcheck: null"。
我尝试过的事情:
- 将child更改为uname(另一个String变量)。数据按预期发布到实时数据库中。
- 在userID周围添加""。不确定我认为它会做什么,但值得一试。
- 将所有字符串和底部代码放在onComplete方法中。
我以前曾使它正常工作,但不确定如何做到的,所以我知道这是可能的。谢谢!
更新:
我将变量更改为全局变量,不再使用final。现在它按预期工作了。
public class YourClassName {
String fullName;
public void signUp() {
// 在这里的onComplete方法中使用了userID
}
}
英文:
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);
答案1
得分: 1
好的,以下是翻译好的内容:
看起来你正在尝试在onComplete
回调之外访问userID
,但这样是行不通的。
原因是身份验证是异步进行的,而在你的reference.child(userID).setValue(newUser)
运行时,userID = newUserID.getUid()
尚未运行。如果你在这些代码行上使用调试器设置断点,或者在它们上面添加一些日志记录,你可以最容易地看到这一点。
任何需要UID的代码都需要在用户登录完成时调用的onComplete
内部,或者从那里调用。
所以:
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);
...
}
});
另请参见:
- https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519,这是关于实时数据库的,但涉及相同的问题,并包含更详细的解释和示例。
- https://stackoverflow.com/questions/62054762/unable-to-get-firebase-current-user-in-kotlin/62055178#62055178。
英文:
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:
- https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519, which is for the Realtime Database, but deals with the same problem and contain a longer explanation, and examples.
- https://stackoverflow.com/questions/62054762/unable-to-get-firebase-current-user-in-kotlin/62055178#62055178
答案2
得分: 0
所以我将我的变量设为了全局变量。稍有小问题。编辑已更新在帖子中。
英文:
So I made my variables global. Slight hiccup. Edits are in the updated post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论