firebase - FLUTTER 应用程序的 Firestore 中的 ValueEventListener 等效项是什么
问题描述
更新!!
假设我有一个名为collection_name且文档 ID为 signed_user_id的集合。我正在尝试侦听子节点signed_user_id并仅在该文档signed_user_id更改时才执行一项任务。它正在工作,但在尝试侦听文档更改时调用了两次。
_listenForHtmlContentUpdate() {
widget.firestore
.collection('collection_name')
.document('signed_user_id')
.snapshots()
.listen((event) {
print('object');
print(event.data);
});
在第二种情况下,假设我有名为html_doc的内部集合和内部文档。因此,我想在html_doc文档字段更改时进行监听。当我使用多个集合和文档时,此功能不起作用。我的意思是在尝试收听内部html_doc文档更新/更改时。那么,如何使用flutter监听内部文档的变化呢?
_listenForHtmlContentUpdate() {
widget.firestore
.collection('collection_name')
.document('signed_user_id')
.collection('html')
.document('html_doc')
.snapshots()
.listen((event) {
print('object');
print(event.data);
});
}
Firestore 更新调用两次案例:根据文档,您的应用程序中的本地写入将立即调用快照侦听器。这是因为一个称为“延迟补偿”的重要功能。当您执行写入时,您的侦听器将在数据发送到后端之前收到新数据的通知。
检索到的文档具有 metadata.hasPendingWrites 属性,该属性指示文档是否具有尚未写入后端的本地更改。您可以使用此属性来确定快照侦听器接收到的事件源。这就是调用两次的原因。
_listenForHtmlContentUpdate() {
print('_listenForHtmlContentUpdate *****');
widget.firestore
.collection('resume')
.document('signed_user_id')
.snapshots()
.listen((event) {
// check and perform the task accordingly.
var source = event.metadata.hasPendingWrites ? "Local" : "Server";
print(source);
});
解决方案
该方法snapshots()
等效于ValueEventListener
,两者都会实时监听任何变化。但在 Firestore 中,您无法监听文档中的单个字段。snapshot()
如果文档中发生任何更改,将提供实时更新。
推荐阅读
- nginx - nginx.conf 忽略了 nginx-ingress 配置映射片段
- group-by - sas中每个不同观察的平均___?
- matlab - .mat 文件不在脚本中打开
- vb.net - Socket IO 2.0 不会从 VB.NET windows 服务连接
- typescript - 高阶装饰器功能的打字稿签名
- scala - 火花换行号
- javascript - 在 react/redux 中,在 store prop 上调用 map() 会编辑 store 本身中的 prop
- r - 在 R 编程语言中使用 DTW 作为距离度量来实现 Knn
- java - Maven 项目在 IntelliJ Idea 中无法正确打开
- ios - 仅在点击时在 UIPickerView 中选择多行(而不是在滚动到一行时)