【问题标题】:flutter-firestore: how to delete item n days old from listviewflutter-firestore:如何从列表视图中删除 n 天前的项目
【发布时间】: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 年的列表项?

【问题讨论】:

    标签: flutter listview google-cloud-firestore


    【解决方案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 变量中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多