DEV Community

AJALA MAYOWA FELIX
AJALA MAYOWA FELIX

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️