繁体   English   中英

如何避免第一次从 flutter 中的 Firestore 满载内容

[英]How to avoid full load content at first time from firestore in flutter

仅当集合中有任何更改时,我才想收听内容。 但是当我尝试StreamSubscription时,所有内容都是第一次加载。

第一次如何避免满载?

 static StreamSubscription<dynamic> listenMessageChange(Function contentChange) {
    return Firestore.instance
        .collection('contents')
        .snapshots()
        .listen((data) {
          List<Message> changedContents = [];
          data.documentChanges.forEach((change) {
            changedContents.add(Content.createFromMap(change.document.data));
          });
          contentChange(changedContents);
    }, cancelOnError: false);
  }

在侦听实时更新时,首先将检索所有数据。 避免完全加载内容的唯一方法是使用方法limit()这样您就不会在第一次加载时得到所有内容。 之后,您可以检查更改/添加/删除的文档:

Firestore.instance
        .collection('contents')
        .snapshots()
        .listen((data) {
      data.documentChanges.forEach((res) {
      if (res.type == DocumentChangeType.added) {
        print("added");
        print(res.document.data);
      } else if (res.type == DocumentChangeType.modified) {
        print("modified");
        print(res.document.data);
      } else if (res.type == DocumentChangeType.removed) {
        print("removed");
        print(res.document.data);
      }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM