我无法从Firestore检索数据

时间:2020-01-11 17:20:23

标签: java android firebase nullpointerexception google-cloud-firestore

我正在使用Android应用程序,并且使用Firestore来存储我的用户及其帖子。我想在RecyclerView中的Feed上显示Firestore的帖子。我使用查询和分页,因此每次我将RecyclerView滚动到末尾时,只会检索10条帖子。我希望将检索到的帖子存储在列表中,这是我尝试的方式:

private void postsRetriever(){
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        Query first = db.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid().toString()).collection("posts").limit(10);
        first.get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {
                        DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
                                .get(documentSnapshots.size() -1);

                        Query next = db.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().toString()).collection("posts")
                                .startAfter(lastVisible)
                                .limit(10);

                        next.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                for(DocumentSnapshot documentSnapshot:task.getResult()) {
                                    Post post = documentSnapshot.toObject(Post.class);
                                    list.add(post);
                                }
                            }
                        });
                    }
                });
    }

问题是每次我尝试使用List时,list.get(pos)上都会出现NullPointerException。:

viewHolder.title.setText(list.get(position).getTheTitle());

0 个答案:

没有答案