0

I'm using BehaviorSubject as a Stream controller.

In one of my functions, I want to .add more items only in case the Stream is empty of events.

  @override
  Future<void> fetchNextOverviewPolls() async {
    if (await _pollOverviewStreamController.isEmpty) return; // My Problem

    final lastDoc = await _pollOverviewStreamController.last;
    final querySnapshot =
        await _overviewPollsRef.startAfterDocument(lastDoc).limit(5).get();
    for (final doc in querySnapshot.docs) {
      _pollOverviewStreamController.add(doc);
    }
  }

The isEmpty property returns a value in case the Stream ends. I want to check it when the Stream is still running.

How do I do that?

genericUser
  • 4,417
  • 1
  • 28
  • 73
  • something like this? `Stream addOtherIfParentIsEmpty(Stream parent, Stream other) async* { var empty = true; await for (final item in parent) { yield item; empty = false; } if (empty) { yield* other; } }` - i dont understand what you really mean by: *"I want to check it when the Stream is still running."* – pskink May 12 '22 at 14:34
  • Is something like this possible? `final controller = StreamController(); controller.add(0); if (controller.stream.isEmpty) { // <-- is that possible? controller.add(1); } controller.close();` – genericUser May 12 '22 at 15:09
  • Issue solved, `BehaviorSubject` supports `hasValue`. @pskink appreciate your help. I have another issue if you can help https://stackoverflow.com/questions/72214735/flutter-bloc-firestore-pagination-using-multiple-querysnapshot-listeners-streame – genericUser May 12 '22 at 15:15

1 Answers1

1

BehaviorSubject supports hasValue.

In the above case, use this line instead:

if (_pollOverviewStreamController.hasValue) return;
genericUser
  • 4,417
  • 1
  • 28
  • 73