我有一个侦听器,它可以正常进行任何更改,但是我无法获得更改的类型,即删除,添加或修改
Firestore.instance.collection('groups').snapshots().listen((data) {
data.documentChanges.forEach((change) {
print('documentChanges ${change.document.data}');
});
});
答案 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