How to stop listening the onValue.listen in flutter?

In flutter for firebase database as a backend, We're using onValue stream for listening to the data using onValue.listen but how can we stop the listening after complete our need.


First we declare the:
    StreamSubscription<Event> _testSubscription;


Pass the onValue stream:

  _testSubscription =  databaseReference.child('user').onValue.listen((Event evetn){

            print('The value of snapshot :${event.snapshot.value}

});


And apply _testSubscription.cancel();   whenever you want to stop listening the data.

Comments