java - 在 Firestore 中找不到文档数据,返回 null

标签 java android firebase android-fragments google-cloud-firestore

我似乎无法将文本放入 TextView 中。数据来自 Firestore 数据库。当我运行它时,TextView 是空白的并返回 null。以下是我到目前为止所做的事情。有人可以看一下吗?你可以在这里看到我的数据库结构:[数据库结构图][1]

public class ProfileFragment extends Fragment {

    TextView firstNameText, age;
    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
        firstNameText = rootView.findViewById(R.id.profile_firstName);
        age = rootView.findViewById(R.id.profile_age);
        getUserInfo();

        return rootView;

    }

    public void getUserInfo() {
        db.collection("Users").document(mAuth.getCurrentUser().getEmail()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {

                    DocumentSnapshot documentSnapshot = task.getResult();
                    String firstName = documentSnapshot.getString("Firstname");
                    Log.d(TAG, "Printing docData: " + documentSnapshot.getData());
                    firstNameText.setText(firstName);

                }
            }
        });
    }
}

最佳答案

我猜是这样的

mAuth.getCurrentUser().getEmail()

就是这里的问题。尝试将用户的电子邮件放入字符串中

mAuth = FirebaseAuth.getInstance();
     final FirebaseUser theUser = mAuth.getCurrentUser();
     if (theUser !=null)
     String UID = theUser.getEmail().toString();

并替换

 mAuth.getCurrentUser().getEmail()

带有 UID 。

关于java - 在 Firestore 中找不到文档数据,返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071016/

相关文章:

java - 如何测试junit中是否抛出并捕获异常

java - 如何获取对话框大小?

java - 当用户从图库上传图像并在textView中显示它们时,如何获取图像名称?

java - WeakReference#get() 什么时候开始返回 null?

android - 通过 1.1.3 - Proguard

java - 如何允许桌面应用程序在同一台机器上调用 Java 应用程序的方法

java - 为什么我需要在 BufferedReader 中捕获 close() 异常,但在 PrintWriter 中不需要?

ios - reloadData() 不更新父 TableView

json - firestore授权规则: for google signed in client

android: 库模块中的 fcm