Error in flutter: Document references must have an even number of segments

纵饮孤独 提交于 2019-12-10 11:57:07

问题


I have a collection named memssages and have to find document where field begin is equal to false. Code is like below.

Future<String> getRoomID() async {
  QuerySnapshot snapshot = await sl.get<FirebaseAPI>().getFirestore()
    .collection('messages')
    .where('begin',isEqualTo: false).getDocuments();
  if(snapshot.documents.length==0){
    return '';
  } else {
    Random random = Random();
    DocumentSnapshot document = snapshot.documents[random.nextInt(snapshot.documents.length)];
    return document.documentID;
  }
}  

But It occurs fatal error like my post title.

java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but messages has 1

What is wrong with me? I was suffered whole day...

Database ScreenShot


回答1:


Collection Reference will have odd number of segment, Document Reference will have even number of segment.

Firestore data structure are: Collection - document - Collection - document - Collection - document

For your case, you are try to call getDocument() from collectionReference(odd segment), however your code after called getDocument() are code belong to getDocument from documentReference(even segment). Hence, the error told you your reference need even number of segment



来源:https://stackoverflow.com/questions/54685662/error-in-flutter-document-references-must-have-an-even-number-of-segments

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