cachapa / firedart

A dart-native implementation of the Firebase Auth and Firestore SDKs
https://pub.dev/packages/firedart
Apache License 2.0
174 stars 62 forks source link

Stream listen is not called when limit is added in Firestore query in Dart / Flutter #69

Open kamleshwebtech opened 3 years ago

kamleshwebtech commented 3 years ago

Stream listen is not called when limit is added in Firestore query in Dart / Flutter

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

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()}");

  });

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).

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).

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.

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

peterBrxwn commented 3 years ago
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()}");

  });

Sorry to ask an unrelated question but please how do you return Stream<QuerySnapshot> from Firedart. To the best of my knowledge, firedart doesn't have the QuerySnapshot data type (and it will save me a lot of stress if I can return this data type)

DylanKarimagoko commented 2 years ago

Stream<List> xstream = FirebaseFirestore.instance .collection('messages') .where('encSenderUId', isEqualTo: _loggedInUserId) .where('encReceiverUId', isEqualTo: widget.encUId) .orderBy('messageId', descending: true) .startAt([_startAtTimestamp]) .limit(1) .stream;

use 'List Document' where it says list im try to edit but stackoverflow is not allowing me