【问题标题】:Flutter Firestore how to implement notification panel like facebook/linkedin?Flutter Firestore 如何实现像 facebook/linkedin 这样的通知面板?
【发布时间】:2021-05-03 17:51:02
【问题描述】:

希望创建一个显示所有用户交互事件的通知页面,如下所示

如何实时跟踪文档更新,例如新评论添加到帖子中,状态更新从待处理到接受等.. ?

有没有可用的firestore功能?

如果唯一的解决方案是读取更新的文档,然后比较更改 VS 缓存数据,我该如何实现“实时跟踪”?由于更新是由另一个用户发起的,如何在通知接收方不采取任何动作的情况下触发“文档更新检查”?此外,这听起来像是一种昂贵的方式,因为应用程序需要执行大量读取、写入操作....

【问题讨论】:

    标签: flutter google-cloud-firestore notifications android-notifications


    【解决方案1】:

    您可以使用 Collection/Document-Reference 的 snapshots() 函数。 该函数返回一个流,可以侦听您的集合或文档中的相关更新。 下面的例子来自于flutter的cloud firestore的official docs


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

    【讨论】:

    • 这不是回答问题
    • 很抱歉听到这个消息 - 您可以使用 QuerySnapshot 的 docChanges-property。像这样: CollectionReference users = FirebaseFirestore.instance.collection('users'); users.snapshots().listen((QuerySnapshot event) { event.docChanges.forEach((change) { // 用 change 做一些事情 }); 希望这会有所帮助,否则如果你能解释为什么这不起作用,我会很高兴给你:)
    • 如果添加了新文档怎么办? docChanges 可以跟踪新文档吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多