Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
synstin opened this issue Nov 18, 2023 · 10 comments
Open

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

synstin opened this issue Nov 18, 2023 · 10 comments
Assignees
Labels
bug Something isn't working needs author feedback Further information or answer is requested by author

Comments

@synstin
Copy link

synstin commented Nov 18, 2023

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 synstin added the bug Something isn't working label Nov 18, 2023
@gdelataillade
Copy link
Owner

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 gdelataillade added the needs author feedback Further information or answer is requested by author label Nov 19, 2023
@synstin
Copy link
Author

synstin commented Nov 20, 2023

@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
Copy link
Owner

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
Copy link
Owner

Hey @synstin

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

@synstin
Copy link
Author

synstin commented Nov 21, 2023

@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
Copy link
Owner

@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
Copy link
Owner

Hi @synstin

Any updates on this issue ?

@Flash0509
Copy link

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 gdelataillade removed the needs author feedback Further information or answer is requested by author label Jan 12, 2024
@gdelataillade
Copy link
Owner

gdelataillade commented Mar 11, 2024

Similar issue: #153
Status: Work in progress

@gdelataillade
Copy link
Owner

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 gdelataillade added the needs author feedback Further information or answer is requested by author label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs author feedback Further information or answer is requested by author
Projects
None yet
Development

No branches or pull requests

3 participants