如何仅监听已更改的文档

huangapple go评论62阅读模式
英文:

How to listen only changed documents

问题

我已经使用以下代码来监听Firestore集合的变化,

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((result) async {

});

当Firestore中的Countries集合中的文档发生变化时,我希望result值应该只包含已更改的文档。但是,result值包含了整个集合的数据。

如何只获取result值中的已更改文档。

我正在使用Flutter Firestore。

英文:

I have used below code to listen Firestore collection change,

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((result) async {

});

When a document change in Countries collection on Firestore. I expect result value should have only changed document. But result value has all the collection data.

How to get only changed document in result value.

Im using Flutter Firestore.

答案1

得分: 1

以下是您潜在的答案:

有3种可能发生的文档更改类型:

  1. 新文档已添加
  2. 文档已更新
  3. 文档已删除

以下是如何处理它们的示例:

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  snapshot.docChanges.forEach((DocumentChange change) {
    if (change.type == DocumentChangeType.added) {
      // 处理已添加的文档
      print("已添加的文档: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.modified) {
      // 处理已修改的文档
      print("已修改的文档: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.removed) {
      // 处理已删除的文档
      print("已删除的文档: ${change.doc.data()}");
    }
  });
});

另一种方法是,如果您只想检查已更新的文档:

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  var modifiedDocuments = snapshot.docChanges
      .where((change) => change.type == DocumentChangeType.modified)
      .map((change) => change.doc)
      .toList();

  // 处理已修改的文档
  modifiedDocuments.forEach((modifiedDocument) {
    // 对已修改的文档进行操作
    print('已修改的文档: ${modifiedDocument.data()}');
  });
});

这是相关的参考链接:

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentChange

英文:

Here is your potential answer
There are 3 types of changes can happen for document

  1. New Doc added
  2. Doc updated
  3. Doc deleted

Here is example how to handle all of them :

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  snapshot.docChanges.forEach((DocumentChange change) {
    if (change.type == DocumentChangeType.added) {
      // Handle added document
      print("Added document: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.modified) {
      // Handle modified document
      print("Modified document: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.removed) {
      // Handle removed document
      print("Removed document: ${change.doc.data()}");
    }
  });
});

Another way is if you want to check for updated docs only

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  var modifiedDocuments = snapshot.docChanges
      .where((change) => change.type == DocumentChangeType.modified)
      .map((change) => change.doc)
      .toList();

  // Process the modified documents
  modifiedDocuments.forEach((modifiedDocument) {
    // Do something with the modified document
    print('Modified document: ${modifiedDocument.data()}');
  });
});

Here is the reference for that

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentChange

答案2

得分: 1

对于Flutter/Dart中的Firestore,您只能知道已更改的文档。
以下是在Flutter/Dart中演示如何仅过滤出已更改文档的示例。

firestoreInstance
      .collection('Countries')
      .snapshots()
      .listen((event) {
    for (var change in event.docChanges) {
      switch (change.type) {
        case DocumentChangeType.added:
          print("New Countries: ${change.doc.data()}");
          break;
        case DocumentChangeType.modified:
          print("Modified Countries: ${change.doc.data()}");
          break;
        case DocumentChangeType.removed:
          print("Removed Countries: ${change.doc.data()}");
          break;
      }
    }
  });

这是官方文档链接:
https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots

英文:

For Flutter/Dart Firestore, you can know only changed document.
Here's an example in Flutter/Dart that demonstrates how to filter out only the changed documents.

  firestoreInstance
        .collection('Countries')
        .snapshots()
        .listen((event) {
      for (var change in event.docChanges) {
        switch (change.type) {
          case DocumentChangeType.added:
            print("New Countries: ${change.doc.data()}");
            break;
          case DocumentChangeType.modified:
            print("Modified Countries: ${change.doc.data()}");
            break;
          case DocumentChangeType.removed:
            print("Removed Countries: ${change.doc.data()}");
            break;
        }
      }
    });

Here is official documentation link
https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots

huangapple
  • 本文由 发表于 2023年6月2日 00:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定