Flutter Firestore how to implement notification panel like facebook/linkedin?

Looking to create a notification page that display all the events of user interaction like below

enter image description here

how can i track document update in realtime, e.g. new comment added to a post, status update from pending to accept etc .. ?

Is there any firestore function available ?

If the only solution is to read the updated doc and then compare the changes VS cache data, how can i achieve "real time tracking"? Since the update is initiated by another user, how to trigger the "doc update check" without any action by the notification receiver? Also this sounds like an expensive way as the app need to do a lot of read, write operations ....

1 answer

  • answered 2021-05-03 18:26 Tobias Weiss

    You can use the snapshots()-function of the Collection/Document-Reference. The function returns a stream which can be listened for concerningupdates within your collection or document. Below example is from the official docs of cloud firestore for flutter:


    class UserInformation extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        CollectionReference users = FirebaseFirestore.instance.collection('users');
        return StreamBuilder<QuerySnapshot>(
          stream: users.snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) {
              return Text('Something went wrong');
            }
    
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Text("Loading");
            }
    
            return new ListView(
              children: snapshot.data.docs.map((DocumentSnapshot document) {
                return new ListTile(
                  title: new Text(document.data()['full_name']),
                  subtitle: new Text(document.data()['company']),
                );
              }).toList(),
            );
          },
        );
      }
    }