gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

Bad state: Stream has already been listened to. #110

Closed synstin closed 2 months ago

synstin commented 1 year ago

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

gdelataillade commented 1 year ago

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.

synstin commented 1 year ago

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

gdelataillade commented 1 year ago

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 commented 1 year ago

Hey @synstin

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

synstin commented 1 year ago

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

gdelataillade commented 1 year ago

@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 commented 11 months ago

Hi @synstin

Any updates on this issue ?

Flash0509 commented 10 months ago

Could this perhaps help you?

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

Similar issue: #153 Status: Work in progress

gdelataillade commented 7 months ago

Hi @synstin @Flash0509

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