Error in flutter: Document references must have an

2019-09-01 02: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