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 天前的项目的主要内容,如果未能解决你的问题,请参考以下文章