如何在Flutter中使用Cloud Firestore知道DocumentChange的更改类型

时间:2019-10-14 03:48:19

标签: firebase flutter dart google-cloud-firestore

我有一个侦听器,它可以正常进行任何更改,但是我无法获得更改的类型,即删除,添加或修改

Firestore.instance.collection('groups').snapshots().listen((data) {
        data.documentChanges.forEach((change) {
          print('documentChanges ${change.document.data}');
        });
      });

1 个答案:

答案 0 :(得分:3)

根据此Google Cloud Firestore API documentation,您可以使用DocumentChange来访问getType()的类型

对于Flutter插件而言,其属性为type(您可以在此处检查插件的DocumentChange类:document_change.dart)。它包含enum属性的type

/// An enumeration of document change types.
enum DocumentChangeType {
  /// Indicates a new document was added to the set of documents matching the
  /// query.
  added,

  /// Indicates a document within the query was modified.
  modified,

  /// Indicates a document within the query was removed (either deleted or no
  /// longer matches the query.
  removed,
}

/// The type of change that occurred (added, modified, or removed).
final DocumentChangeType type;

因此,您应该可以:

change.type == DocumentChangeType.added