firebase - 如何在flutter中避免第一次从firestore中满载内容
问题描述
仅当集合中有任何更改时,我才想收听内容。但是当我尝试时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);
}
});
推荐阅读
- nativescript - 根据设备高度在 ScrollView 中动态调整 Nativescript ContentView 的大小
- python - 检查numpy数组的任何元素是否不在列表中?
- java - 在 NetBeans 的同一个 java 应用程序项目中具有多个具有 Main() 函数的 jar 文件
- swift - swift如何将日期组件转换为双精度以获得上传速度
- reactjs - 另一个组件中的状态更改
- python - 在终端中导入 Python 库
- html - 使用html从外部源更改按钮的颜色?
- anychart - Anychart 缩放非地图图表类型
- javascript - 使用 React JavaScript 的 Spring-Boot
- android - onActivityResult 返回空数据