【问题标题】:How to know the type of change of DocumentChange using Cloud Firestore in Flutter如何在 Flutter 中使用 Cloud Firestore 了解 DocumentChange 的更改类型
【发布时间】:2019-10-14 03:48:19
【问题描述】:
我有一个监听器,可以监听任何发生的更改,它工作正常,但我无法获取更改的类型是删除、添加还是修改
Firestore.instance.collection('groups').snapshots().listen((data) {
data.documentChanges.forEach((change) {
print('documentChanges ${change.document.data}');
});
});
【问题讨论】:
标签:
firebase
flutter
dart
google-cloud-firestore
【解决方案1】:
根据这个Google Cloud Firestore API documentation,可以用getType()访问DocumentChange的类型
如果 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