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

Receive Event two times #29

Closed
sumitb247software opened this issue Nov 5, 2019 · 10 comments
Closed

Receive Event two times #29

sumitb247software opened this issue Nov 5, 2019 · 10 comments

Comments

@sumitb247software
Copy link

Hi @marcojakob
I'm using event_bus: ^1.1.0 in flutter.

From one page i'm fire event like eventBus.fire(CustomEvents.START_MEDIA_SYNC_EVENT);
and i'm listening event like

void getObserverData() {
eventBus.on().listen((event) => {print(event.toString())});
}
getObserverData() method is called from second screen build method.

I got the events two times.
I initialize bus like this EventBus eventBus = new EventBus(); in one dart file.

Can please help me?

Thanks with best regards,
Sumit Bhondave.

@marcojakob
Copy link
Owner

Your getObserverData() method is a strange thing. It doesn't return anything but installs a listener. So, if you call it two times, the listener is installed two times and you will receive the events two times.

@sumitb247software
Copy link
Author

sumitb247software commented Nov 5, 2019

HI @marcojakob
Thank you for your response.
But where to install listener once in flutter.

Can please help me?

Thanks with best regards,
Sumit Bhondave.

@marcojakob
Copy link
Owner

marcojakob commented Nov 5, 2019

Just include this line somewhere in the program and don't call it multiple times (except if you want to listen to it in multiple components):

eventBus.on().listen((event) => {print(event.toString())});

@sumitb247software
Copy link
Author

HI @marcojakob
Issue is resolved. I install listener in initState().
Thank you.

Thanks with best regards,
Sumit Bhondave.

@xhidnoda
Copy link

i think install the eventBus.on on initState() cause problem... in my case i put in the initState() and when push to another pageFlutter the initState() is calling again my eventBus.on.
So i receive event two times. :( where is the correct way to put the listener of eventBus.on?

@marcojakob
Copy link
Owner

@xhidnoda I don't have experience with Flutter. But I know there is a dispose() function. If you register something in initState() you should probably cancel the subscription to the event bus in dispose().

@xhidnoda
Copy link

Ok i declare this:

StreamSubscription busEventListener;

Add listener like this in initState() :

busEventListener = eventBus.on<ClassEventResp>().listen((event) {
      _parseResponse(event.message);
    });

In dispone():

doctorEventListener.cancel();

But is not working..what is wrong? please help!

@marcojakob
Copy link
Owner

Sorry @xhidnoda. I can't provide much support here. But you should try debugging your application (or use logging) to see how many times you are registering for the same event.

@xhidnoda
Copy link

Sorry error of my code. I calledinitState()from my ListView many times. So I put the code logic outside the ListView, and it works perfect.

@devmanzur
Copy link

devmanzur commented Mar 7, 2021

call eventBus.destroy(); on your dispose method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants