flutter-firestore:如何从列表视图中删除 n 天前的项目

Posted

技术标签:

【中文标题】flutter-firestore:如何从列表视图中删除 n 天前的项目【英文标题】:flutter-firestore: how to delete item n days old from listview 【发布时间】:2021-06-07 05:50:40 【问题描述】:

我希望在我的 flutter-firestore 应用程序的页面中自动从列表视图中删除超过 10 天的项目。这是我创建该列表视图的代码。

body: StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('notifs')
          .orderBy('notifTimestamp', descending: true)
          .snapshots(),
      builder:
          (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) 
        if (!snapshot.hasData) 
          return Text('Loading...');
        
        return ListView.builder(
            itemCount: snapshot.data.docs.length,
            itemBuilder: (_, index) 
              return Card(
                child: Column(
                  children: [
                    ...
 ]))

如何自动删除超过 10 年的列表项?

【问题讨论】:

【参考方案1】:

'notifTimestamp' 保存在Date 变量中的Firestore 中,来自Dart

    DateTime tenDaysOld=DateTime.now().subtract(Duration(days: 10));

    FirebaseFirestore.instance
          .collection('notifs')
          .orderBy('notifTimestamp', descending: true)
          .startAt([tenDaysOld]).snapshots();

【讨论】:

谢谢阿蒙,我用这段代码保存了'notifTimestamp''notifTimestamp': FieldValue.serverTimeStamp()。如何将这个 'notifTimestamp' 保存在 date 变量中?

以上是关于flutter-firestore:如何从列表视图中删除 n 天前的项目的主要内容,如果未能解决你的问题,请参考以下文章

如何从列表视图元素通知事件列表视图的父适配器?

如何从列表视图按钮单击将多个数据添加到数组列表?

如何使用按钮从集合视图(网格视图)切换到表格视图(列表视图)?

如何从列表视图中的标记颜色开始?

如何从单击项目打开列表视图?

如何从列表视图中获取值