Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if any active Stream has already been listened to?

Is there any way to check if there is any active Stream that has already been listened to or not in Dart/Flutter?

Suppose there is a Stream that has been listened to on one Screen. But, I need to listen to that Stream on another screen. So that I need to check if there is any active stream that has been already listened to or not.

like image 583
Santo Shakil Avatar asked Oct 13 '25 09:10

Santo Shakil


1 Answers

You've to use StreamController in order to check if the stream's been listened to already by using StreamController's handy hasListener getter.

Here's minimal sample code to understand:

StreamController controller = StreamController();
controller.addStream(getNumbersAsStream());

controller.stream.listen((e) => print(e));
if (!controller.hasListener) {
  controller.stream.listen((e) => print(e));
} else {
  print("Stream has already been listened");
}
like image 178
Sahil Sonawane Avatar answered Oct 15 '25 01:10

Sahil Sonawane