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

238

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(),
        );
      },
    );
  }
}
    

        
Share:
238
icantcode
Author by

icantcode

Updated on December 29, 2022

Comments

  • icantcode
    icantcode over 1 year

    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 ....

  • icantcode
    icantcode almost 3 years
    this is not answering the question
  • Tobias Weiss
    Tobias Weiss almost 3 years
    Sorry to hear that - you could use docChanges-property of the QuerySnapshot. Like so: CollectionReference users = FirebaseFirestore.instance.collection('users'); users.snapshots().listen((QuerySnapshot event) { event.docChanges.forEach((change) { // Do something with change }); Hope this helps, otherwise I would be pleased if you could explain why this does not work for you :)
  • icantcode
    icantcode almost 3 years
    what if new document added? can docChanges track new document?