在 Dart/Flutter 的 Firestore 查询中添加限制时,不会调用流监听 [英] Stream listen is not called when limit is added in Firestore query in Dart / Flutter

查看:23
本文介绍了在 Dart/Flutter 的 Firestore 查询中添加限制时,不会调用流监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Dart/Flutter 的 Firestore 查询中添加限制时,不会调用流监听

final _startAtTimestamp = Timestamp.fromMillisecondsSinceEpoch(DateTime.parse('2000-01-01 01:01:01.001').millisecondsSinceEpoch);

我在我的 initState() 函数中使用了以下代码:

I have used below code in my initState() function:

@override
void initState() {
    //
    super.initState();

    Future.delayed(Duration(seconds: 1), () async {
      // 
      Stream<QuerySnapshot> xstream = FirebaseFirestore.instance
          .collection('messages')
          .where('encSenderUId', isEqualTo: _loggedInUserId)
          .where('encReceiverUId', isEqualTo: widget.encUId)
          .orderBy('messageId', descending: true)
          .startAt([_startAtTimestamp])
          .limit(1)
          .snapshots();

      xstream.listen((event) {
        //
        print('Hiii, I am listening..');

        //
      }, onDone: () {
        print("Task Done single");
      }, onError: (error) {
        print("Some Error ${error.toString()}");
      });

    });

}

案例 1:如果我 DO NOT 在查询中使用 limit() 然后 listen 消息在第一次呈现屏幕时打印一次,并且当 Firestore 流中的某些内容发生更改(例如,添加新文档/删除文档)时,也会调用 listen.

Case 1: If I DO NOT use limit() in query then listen message is printed once when screen is rendered first time and also listen is called when something is changed in Firestore stream (eg. added new document / removed a document).

案例 2:如果我在查询中使用 limit() 然后 listen 消息在第一次呈现屏幕时打印一次,但 listen在 Firestore 流中的某些内容发生更改时调用(例如,添加新文档/删除文档).

Case 2: If I use limit() in query then listen message is printed once when screen is rendered first time but listen is NOT called when something is changed in Firestore stream (eg. added new document / removed a document).

我必须使用 limit() 根据设置的 orderBy 来获取一个文档.我 100% 确定 limit() 不允许流监听任何事件.

I have to use limit() to fetch one document according to set orderBy. I am 100% sure that limit() is not allowing stream to listen any event.

Firestore 结构如下:

请建议我如何使用 limit 和 `listen' 来解决这个问题.非常感谢.

Kindly suggest how can I use limit with `listen' to fix this issue. Thanks a lot.

推荐答案

首先处理您的查询(包括 .limit(1)),然后附加侦听器 - 因此您正在侦听对一份,而且只有一份文件.只有对该文档的更改才会导致更新.它不会重新处理查询.您是否正在尝试收听最新的文档?

You query (including .limit(1)) is processed FIRST, THEN the listener attached - so you are listening for changes to ONE, and ONLY ONE document. Only changes to THAT DOCUMENT will cause updates. it DOES NOT re-process the query. Are you TRYING to listen for the MOST RECENT document?

这篇关于在 Dart/Flutter 的 Firestore 查询中添加限制时,不会调用流监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆