Listen to changes in Firestore

Hi there,

I am looking for a solution to listen to document changes in firestore.
I guess I am using a Stream/StreamBuilder but I don’t now how.
Below you can find my latest code snippet:

-> Firestore instance

final up = Firestore.instance;

-> Stream getting data from firebase

Stream<QuerySnapshot> getUpdate() {
  return up.collection('news').snapshots();

-> actually method

checkChanges() async {
    stream: getUpdate(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      getUpdate().listen((data) {
        data.documentChanges.forEach((change) { 
          return print('documentChanges: ${}');

I hope anyone can help me!
Thanks in advance.

Actually I don’t know what you want to do exactly because inside the builder function, you need to return a Widget and whatever you want to return, print() function returns void. If you try to return the result of print() you would get nothing returned.

Your StreamBuilder usage seems fine tho.

1 Like

Ok, you are right. ‘Return print’ is a bad example.
I wanna listen to changes in firestore (with the posted stream builder) and send a push notification (with this plugin: even a document changes. This is my purpose.

I’m sorry n1c0_1202 I haven’t used that package so I’m afraid I can’t give you a proper answer now.

But, in any case, StreamBuilder should be used to build the Widget. I don’t know the mechanism of the package you mentioned but if it works without a widget, just attach a listener to the stream and make it show the notification whenever the stream recognize the change.

Good luck!

It seems to be running.
I changed the StreamBuilder to:

Stream getUpdate() {
  up.collection('news').snapshots().listen((event) {
    event.documentChanges.forEach((change) {
      print('Document has changed');
      return showNotification();      
    print('Error in processing changes');