如何在 android 中使用 firebase 接收额外信息

How to receive additional information by using firebase in android

当用户第一次注册时,我通过以下代码使用sendEmailVerification

 FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
                            String uid = mFirebaseUser.getUid();
                            FirebaseFirestore db = FirebaseFirestore.getInstance();
                            mFirebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        startToast("Plz go to your mail box.");
                                        UserAccount account = new UserAccount(uid, strEmail, strPwd);
                                        db.collection("Basic_users").document(mFirebaseUser.getUid()).set(account).addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void avoid) {
                                                finish();
                                            }
                                        })
                                                .addOnFailureListener(new OnFailureListener() {
                                                    @Override
                                                    public void onFailure(@NonNull Exception e) {
                                                        startToast("failed to register");
                                                    }
                                                });

这里是useAccount.java

public class UserAccount {
    private String idToken; 
    private String strEmail;
    private String strPwd;

    public void setIdToken(String idToken) {
        this.idToken = idToken;
    }

    public void setStrEmail(String strEmail) {
        this.strEmail = strEmail;
    }

    public void setStrPwd(String strPwd) {
        this.strPwd = strPwd;
    }

    public String getIdToken() {
        return idToken;
    }

    public String getStrEmail() {
        return strEmail;
    }

    public String getStrPwd() {
        return strPwd;
    }

    public UserAccount(String idToken, String strEmail, String strPwd) {
        this.idToken = idToken;
        this.strEmail = strEmail;
        this.strPwd = strPwd;
    }

Firebase information after signing up

如您所见,所有信息均已准确接收。
但我想在用户首次使用该应用程序时收到其他信息。 所以我尝试通过以下代码接收更多信息

  FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) { //IF there's no user information go to registerActiviety
            startActivity(registerActivity.class);
        } else { //else get current user information
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            DocumentReference docRef = db.collection("users").document(user.getUid());
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) { //if document exist
                            Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                        } else { //else go to MemberinitActivity and receive some 
                                 //additional information
                            Log.d(TAG, "No such document");
                            startActivity(MemberinitActivity.class);
                        }
                    } else {
                        Log.d(TAG, "get failed with ", task.getException());
                    }
                }
            });

这里是 memberinfo.java,其中包含我希望收到更多信息。

public class MemberInfo
{
private String idToken; 
private String name;
private String birthday;
private String address;
private String phone_number;

public MemberInfo(String idToken, String name, String birthday,String 
phone_number,String address) {
        this.idToken = idToken;
        this.name = name;
        this.birthday = birthday;
        this.phone_number = phone_number;
        this.address = address;
    }

    public void setIdToken(String idToken) {
        this.idToken = idToken;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getIdToken() {
        return idToken;
    }

    public String getName() {
        return name;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getAddress() {
        return address;
    }

    public String getPhone_number() {
        return phone_number;
    }
}

查看日志后,我发现程序识别出文件存在 但它实际上什么都没有(null) log of "if document exists"

我认为该程序很混乱,因为它们共享相同的 getcurrentuser() 如何分隔两个用户值并接收其他信息?

I think the program was confused since they share the same getcurrentuser()

不,这不是您收不到任何数据的原因。您的代码中的问题在于您正在将数据写入以下位置的 Firestore:

db.collection("Basic_users").document(mFirebaseUser.getUid())
                //

但是您正在尝试从不同的位置读取:

DocumentReference docRef = db.collection("users").document(user.getUid());
                                         //

由于你的截图显示用户存在于Basic_userscollection下,要解决这个问题,请将最后一行代码更改为:

DocumentReference docRef = db.collection("Basic_users").document(user.getUid());
                                           //