How to remove firebase "onChildAdded" listener in dart

20 Views Asked by At

I'm currently using Dart and Firebase and seem to be unable to remove an onChildAdded listener. To add a listener I do the following:

_messagesRef.child(userID).onChildAdded.listen(_onUserMessageAdded);

To remove a listener, the firebase documentation states

Callbacks are removed by calling the off() method on your Firebase database reference. You can remove a single listener by passing it as a parameter to off(). Calling off() on the >location with no arguments removes all listeners at that location. Calling off() on a parent listener does not automatically remove listeners registered on its child >nodes; off() must also be called on any child listeners to remove the callback.

The documentation states that you can call off on a location to remove all listeners. When I try to call off() however, it does not seem to be a method of the DatabaseReference class.
_messagesRef.child(userID).off();
The above code will not compile and gives the error
Error: The method 'off' isn't defined for the class 'DatabaseReference'.

Any help with understanding how to remove a listener would be greatly appreciated.

0

There are 0 best solutions below