Firestore 集合组查询流未更新

nip*_*dha 4 flutter google-cloud-firestore

我正在将 Flutter 与 Firebase Firestore 一起用于移动应用程序。以下集合组流工作并在每次更新任务字段时更新。

Firestore.instance
    .collectionGroup('tasks')
    .snapshots()
    .listen((QuerySnapshot tasksSnapshot) {
  print('task change is triggered');
});
Run Code Online (Sandbox Code Playgroud)

当我使用“array-contains”查询集合组时,查询有效,但对任务的更改不会更新流。

Firestore.instance
    .collectionGroup('tasks')
    .where('assignees', arrayContains: userData.id) // new query line
    .snapshots()
    .listen((QuerySnapshot tasksSnapshot) {
  print('task change is triggered');
});
Run Code Online (Sandbox Code Playgroud)

你能解释这种行为的原因吗?以及如何实时获取集合组查询结果?我应该考虑任何索引设置吗?

nip*_*dha 5

终于知道是怎么回事了。Firebase 通常会输出一条错误消息,其中包含要Exemptions在 Firebase 的“索引”部分中设置所需的 URL 。但是我在使用的时候没有报错collectionGroup().snapshots().listen()

然后我打电话collectionGroup().getDocuments()只是想看看它是否正常工作,并弹出一个带有豁免 URL 的错误。设置豁免后,collectionGroup().snapshots().listen()重新开始工作!

我认为这个技巧对某人有用。