颤动错误:文档引用必须具有偶数个段 [英] Error in flutter: Document references must have an even number of segments

查看:86
本文介绍了颤动错误:文档引用必须具有偶数个段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 memssages 的集合,并且不得不查找其中开始字段等于false的文档。代码如下。

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:无效的文档引用。
文档引用必须具有偶数个段,但是消息
具有1

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...

数据库ScreenShot

推荐答案

集合引用的段数为奇数,
文档引用的段数为偶数段的数量。

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

Firestore数据结构为:
集合-文档-集合-文档-集合-文档

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

您尝试从 collectionReference(奇数段)调用 getDocument(),但是,调用<$ c之后的代码$ c> getDocument()属于 documentReference(偶数段)中的 getDocument 。因此,该错误告诉您您的引用需要偶数个段。

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

这篇关于颤动错误:文档引用必须具有偶数个段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆