首页 > 解决方案 > 我没有从 Cloud Firestore 获取数据

问题描述

我正在设计一个社交媒体应用程序,其中我有其他人的个人资料数据,即用户名、用户邮箱和个人资料图片,并且我还在 dp 后面显示个人资料图片(作为背面图片)。我在云 Firestore 中有登录用户的所有数据,但是当我使用用户的电子邮件和密码登录时,未显示登录用户的数据意味着它没有从云 Firestore 获取数据。这是我的代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         getCurrentUserDetails();
    }

    private void    getCurrentUserDetails()
        {
            try {
                CurrentUserEmail = getCurrentLoggedInUser();
                if (CurrentUserEmail.equals("No user is logged in"))
                {
                Toast.makeText(this, "No user is logged in", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoginPage.class));
                finish();
            }
                else
                {
                    header_progressBar.setVisibility(View.VISIBLE);
                    objectDocumentReference=objectFirebaseFireStore.collection("UserProfileData")
                            .document(CurrentUserEmail);
                    objectDocumentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                        @Override
                        public void onSuccess(DocumentSnapshot documentSnapshot) {
                        header_userName.setText(documentSnapshot.getString("username"));
                        header_userName.setAllCaps(true);
    
                        header_user_email.setText(CurrentUserEmail);
                        String linkOfProfileImage=documentSnapshot.getString("profileimageurl");
                        Glide.with(MainContentPage.this).load(linkOfProfileImage).into(header_ProfilePic);
                            Glide.with(MainContentPage.this).load(linkOfProfileImage).into(header_backgroundProfile);
                            header_progressBar.setVisibility(View.INVISIBLE);
                            
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(MainContentPage.this, "Loading User Details", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
    
            }
    
            catch (Exception e)
            {
                Toast.makeText(this, "MainContentPage:"+e.getMessage(), Toast.LENGTH_SHORT).show();
            }
    
        }
        private String getCurrentLoggedInUser()
        {
            try
            {
                if(objectFirebaseAuth!=null)
                {
                    return objectFirebaseAuth.getCurrentUser().getEmail().toString();
                }
                else
                {
                    return "No user is logged in";
    
                }
            }
            catch (Exception e)
            {
                Toast.makeText(this, "MainContentPage:"+e.getMessage(), Toast.LENGTH_SHORT).show();
                return null;
            }
        }

标签: javaandroid-studiogoogle-cloud-functionsgoogle-cloud-firestore

解决方案


推荐阅读