alarm icon indicating copy to clipboard operation
alarm copied to clipboard

Bad state: Stream has already been listened to.

Open synstin opened this issue 1 year ago • 10 comments

Alarm plugin version 2.1.1

Describe the bug

class HomeController extends GetxController {
  late final StreamSubscription<AlarmSettings> subscription;

  @override
  void onInit() {
    subscription = Alarm.ringStream.stream.listen((event) => print('ring'));
  }

  @override
  void onClose() async {
    subscription.cancel();
  }
}

I subscribe to ringStream when I go to the home page and close the subscription when I leave the page.

However, when I go to the homepage, then back, then go to the homepage, I get the error Bad state: Stream has already been listened to. I don't know why I get that error even though I closed the stream. can you help?

Device info every device

synstin avatar Nov 18 '23 16:11 synstin

Hi @synstin

Thanks for your interest in the package.

Try to convert the Stream to a Broadcast Stream: This allows multiple listeners. You can do this by using the .asBroadcastStream() method on your stream. This is useful if you need multiple parts of your app to listen to the same stream.

subscription = Alarm.ringStream.stream.asBroadcastStream().listen((event) => print('ring'));

Let me know if it fixes you issue.

gdelataillade avatar Nov 19 '23 14:11 gdelataillade

@gdelataillade

The same thing happens when apply asBroadcastStream(). And I only want to keep one stream, and I want to close the stream when I'm done using it. I don't know why I'm still getting that error after calling close.

synstin avatar Nov 20 '23 06:11 synstin

Hi @synstin

Ok I will see if I can change the plugin's stream to fix your issue. In the meantime, can you keep the same stream all the time ?

gdelataillade avatar Nov 20 '23 14:11 gdelataillade

Hey @synstin

Are you sure your subscription.cancel(); is called ?

gdelataillade avatar Nov 20 '23 16:11 gdelataillade

@gdelataillade I'm pretty sure cancel is called. Streams from other plugins ex) FGBG are closing without problems

We really appreciate your attention to the issue and will be using one stream unclosed for now while it is fixed.

synstin avatar Nov 21 '23 01:11 synstin

@synstin

Alright. Just to be sure you can log the onClose method, maybe your controller is not disposed well.

In the meantime, you can make your stream static. This way, you can keep it in the HomeController scope and initialize only once. It's what I did in the example app.

class HomeController extends GetxController {
  static StreamSubscription<AlarmSettings>? subscription;

  @override
  void onInit() {
    subscription ??= Alarm.ringStream.stream.listen((event) => print('ring'));
  }

  @override
  void onClose() async {
    subscription?.cancel();
    print('HomeController: onClose');
  }
}

gdelataillade avatar Nov 21 '23 10:11 gdelataillade

Hi @synstin

Any updates on this issue ?

gdelataillade avatar Dec 18 '23 22:12 gdelataillade

Could this perhaps help you?

if (!Alarm.ringStream.hasListener) {
          print("Alarm STREAMING");
          Alarm.ringStream.stream.listen((_) => print('ring'));
        } else {print("STREAM is already active");}

Flash0509 avatar Jan 11 '24 22:01 Flash0509

Similar issue: #153 Status: Work in progress

gdelataillade avatar Mar 11 '24 17:03 gdelataillade

Hi @synstin @Flash0509

I just released version 3.1.0. It may fix your issues with the stream. Let me know how it goes.

gdelataillade avatar Apr 12 '24 15:04 gdelataillade