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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay