'Flutter check if stream is empty before close end

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?



Solution 1:[1]

BehaviorSubject supports hasValue.

In the above case, use this line instead:

if (_pollOverviewStreamController.hasValue) return;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 genericUser