Firestore-根据字段获取特定文档?

时间:2019-01-01 08:25:27

标签: java android firebase firebase-realtime-database google-cloud-firestore

 mFirestore.collection("FeaturedDeal").whereEqualTo("title","Amazon India").limit(10).orderBy("priority", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.d(TAG, "Error : " + e.getMessage());

                }

                    assert documentSnapshots != null;
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String doc_id = doc.getDocument().getId();
                            FeaturedDeal featuredDeal = doc.getDocument().toObject(FeaturedDeal.class).withDocId(doc_id);
                            featuredDeals.add(featuredDeal);
                            featuredDealAdapter.notifyDataSetChanged();
                        } else if (doc.getType() == DocumentChange.Type.MODIFIED) {
                            String docID = doc.getDocument().getId();
                            FeaturedDeal changedModel = doc.getDocument().toObject(FeaturedDeal.class).withDocId(docID);
                            if (doc.getOldIndex() == doc.getNewIndex()) {
                                // Item changed but remained in same position
                                featuredDeals.set(doc.getOldIndex(), changedModel);
                                featuredDealAdapter.notifyItemChanged(doc.getOldIndex());
                            } else {
                                // Item changed and changed position
                                featuredDeals.remove(doc.getOldIndex());
                                featuredDeals.add(doc.getNewIndex(), changedModel);
                                featuredDealAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex());
                            }
                        } else if (doc.getType() == DocumentChange.Type.REMOVED) {
                            // remove
                            featuredDeals.remove(doc.getOldIndex());
                            featuredDealAdapter.notifyItemRemoved(doc.getOldIndex());
                        }
                    }
                }

        });

App正在崩溃,并将此错误提供给Logcat:

  

java.lang.NullPointerException:尝试在com.example.info.lootbox.Fragments上的空对象引用上调用虚拟方法'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()'。 HomeFragment $ 9.onEvent(HomeFragment.java:313)

2 个答案:

答案 0 :(得分:0)

删除此:

assert documentSnapshots != null;

并添加此检查,而不是在侦听器中声明:

if (e != null) {
  Log.w(TAG, "listen:error", e);
  return;
}

如果出现错误,则应返回侦听器。该错误表明快照中没有数据。

答案 1 :(得分:0)

您必须首先检查快照中是否有可用数据。所以像这样检查。

mFirestore.collection("FeaturedDeal").whereEqualTo("title","Amazon India").limit(10).orderBy("priority", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.d(TAG, "Error : " + e.getMessage());
                   return;
                }else if ( documentSnapshots != null) {

                 for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String doc_id = doc.getDocument().getId();
                            FeaturedDeal featuredDeal = 
            doc.getDocument().toObject(FeaturedDeal.class).withDocId(doc_id);
                            featuredDeals.add(featuredDeal);
                            featuredDealAdapter.notifyDataSetChanged();
                        } else if (doc.getType() == DocumentChange.Type.MODIFIED) {
                            String docID = doc.getDocument().getId();
                            FeaturedDeal changedModel = 
            doc.getDocument().toObject(FeaturedDeal.class).withDocId(docID);
                            if (doc.getOldIndex() == doc.getNewIndex()) {
                                // Item changed but remained in same position
                                featuredDeals.set(doc.getOldIndex(), changedModel);
                                featuredDealAdapter.notifyItemChanged(doc.getOldIndex());
                            } else {
                                // Item changed and changed position
                                featuredDeals.remove(doc.getOldIndex());
                                featuredDeals.add(doc.getNewIndex(), changedModel);
                                featuredDealAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex());
                            }
                        } else if (doc.getType() == DocumentChange.Type.REMOVED) {
                            // remove
                            featuredDeals.remove(doc.getOldIndex());
                            featuredDealAdapter.notifyItemRemoved(doc.getOldIndex());
                        }
                    }
                }

           }

        });