DEV Community


Posted on

Solution: Flutter Firebase realtime listener not cancelling

Firebase realtime database listener does not cancel even when you dispose the widget or explicitly cancel the streamsubscription.
The listener keeps listening underground which increases your billings, causes memory leak and consumes your device resources.

I had this same problem for a while, I could not find any tangible solution on github, stackoverflow and other tech related blogs, till I eventually created a workaround.

Let me walk you through:

To cancel the streamSubscription listener, I simply called the goOffline() method on my database instance.

This method completely cancelled the listener from underground activities.

final database = FirebaseDatabase.instance;
database.goOffline()// to stop your listener
//After canceling the listener, to resume or start other database operations 
database.goOnline() //everything works back better
Enter fullscreen mode Exit fullscreen mode

I put the called database.goOffline() in the dispose state and it works perfectly.

Glad to help

Top comments (0)