【问题标题】: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)。它包含 enumtype 属性:

    /// 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
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 2019-12-01
      • 2021-07-15
      • 2019-07-12
      • 2022-01-17
      • 2022-01-23
      • 1970-01-01
      • 2019-12-21
      相关资源
      最近更新 更多