dart - flutter 错误 : Document references must have an even number of segments

标签 dart flutter google-cloud-firestore fatal-error

我有一个名为 memssages 的集合,必须找到字段 begin 等于 false 的文档。代码如下。

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;
  }
}  

但是它发生了 fatal error ,就像我的帖子标题一样。

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

我这是怎么了?我被折磨了一整天......

Database ScreenShot

最佳答案

Collection Reference 会有奇数段, 文档引用将有偶数个段。

Firestore 的数据结构有: 收藏-文档-收藏-文档-收藏-文档

对于您的情况,您正在尝试调用 getDocument()来自 collectionReference(odd segment) ,但是,您的代码在调用 getDocument() 之后, 属于 getDocument来自 documentReference(even segment) .因此,错误告诉您您的引用需要偶数个段。

关于dart - flutter 错误 : Document references must have an even number of segments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54685662/

相关文章:

监听数据时出现 HttpClientResponse 错误

flutter - 如何在 Flutter 中的文本(表单)字段中从 `FocusNode` 移除焦点

Flutter - 在没有 AppBar 的情况下更改状态栏颜色

node.js - 如何让 Firebase Functions 充当用户而不是管理员?

dart - TextFormField 验证器,错误在 TextFormField 上不可见

flutter - 如何在 flutter 中扫描文档

sqlite - 列表项更改时Flutter ListView不会更新

android-studio - 可以使用 flutter 在 VS Code 和 Android Studio 中启动但不能连接到模拟器

nuxt 上的 Firebase 身份验证监听器

node.js - 在 Cloud Functions for Firebase 中运行 Firestore 批处理