Flutter Firestore Query Nested Subcollections

无人久伴 提交于 2020-01-30 10:48:11

问题


I am trying to query subcollection in Firebase, but I always get an empty list...

This is my query:

Firestore.instance.collection('messages').where('idFrom', isEqualTo: userID).snapshots();

I know that I have subcollection with another subcollection here..

/messages/RWzG98s92mVTZniofez6YoYsNhA3-tT2Q16n1FMZoTNZQOejtWuJdCmD2/RWzG98s92mVTZniofez6YoYsNhA3-tT2Q16n1FMZoTNZQOejtWuJdCmD2/1579394957103

And my question is how to query these types of models?


回答1:


Since Firstore queries are always shallow, you have to build the path to the subcollection to query.

Firestore.instance
    .collection('messages')
    .document(...)   // ID of the nested document
    .collection(...).  // ID of the nested subcollection
    .where('idFrom', isEqualTo: userID);

There is no way to avoid giving those nested IDs. If you can't identify the full path of the subcollection to query, you won't be able to access its documents.



来源:https://stackoverflow.com/questions/59811699/flutter-firestore-query-nested-subcollections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!