Database 如何限制Firebase中日期为Flutter的文档数量
我正在构建一个flutter应用程序,通过firebase cloud firestore获取文档,但我希望它只显示当天编写的文档。就像我添加到集合中一样,它将只返回今天的文档。请帮忙Database 如何限制Firebase中日期为Flutter的文档数量,database,firebase,flutter,dart,google-cloud-firestore,Database,Firebase,Flutter,Dart,Google Cloud Firestore,我正在构建一个flutter应用程序,通过firebase cloud firestore获取文档,但我希望它只显示当天编写的文档。就像我添加到集合中一样,它将只返回今天的文档。请帮忙 Container( height: MediaQuery.of(context).size.height, child: StreamBuilder<QuerySnapshot>( stream: Fires
Container(
height: MediaQuery.of(context).size.height,
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('all').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('.....Loading');
}
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot all = snapshot.data.documents[index];
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
Text('10:00', style: TextStyle(fontSize: 18),),
SizedBox(height: 3,),
Text('05 Sep', style: TextStyle(fontSize: 13),),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Europa league', style: TextStyle(fontSize: 15),),
SizedBox(height: 2,),
Text( '${all['gg']['home']} vs ${all['gg']['away']}', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
容器(
高度:MediaQuery.of(context).size.height,
孩子:StreamBuilder(
流:Firestore.instance.collection('all').snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData){
返回文本(“..…加载”);
}
返回ListView.builder(
滚动方向:轴垂直,
itemCount:snapshot.data.documents.length,
itemBuilder:(上下文,索引){
DocumentSnapshot all=snapshot.data.documents[索引];
返回容器(
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
纵队(
儿童:[
文本('10:00',样式:TextStyle(fontSize:18),),
尺寸箱(高度:3,),
文本('9月5日',样式:文本样式(字体大小:13),),
],
),
纵队(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
Text('Europa league',样式:Text样式(字体大小:15),),
尺寸箱(高度:2,),
文本(${all['gg']['home']}vs${all['gg']['away']}),样式:TextStyle(fontSize:18,fontWeight:fontWeight.w500),
您只需在查询中添加where子句,如下所示(这将提供过去24小时内生成的任何内容):
确保您的数据具有日期,在插入数据时,您可以这样做:
Firestore.instance
.collection("all")
.add({
'data': "data",
'created_date' : FieldValue.serverTimestamp(),
});
使用时间戳?这回答了你的问题吗?我是一个完全的新手,我真的不明白谢谢你Jan,但我尝试过并上传了一个新的文档,但数据库没有返回任何东西。我想要一种情况,我上传一个文档到我的收藏中,它整天都在屏幕上。例如,如果我在星期一早上上传,它只会持续整个周一,直到我为周二编写另一个文档。我的cloud firestore文档中是否有一个时间戳字段?是的,您的cloud firestore文档中需要一个时间戳字段。添加文档时,请确保给它一个日期,在我的示例中,我将该字段称为“created_date”,并指示firestore设置时间戳服务器当前时间戳的值。当您要检索文档时,需要从集合中进行选择(您已经完成),然后添加一个“where子句”,该子句将过滤特定日期范围的结果。在我的示例中,它将过滤集合中具有大于(更新)值的“created_date”的所有文档从这个确切时间算起1天(又名24小时)。
Firestore.instance
.collection("all")
.add({
'data': "data",
'created_date' : FieldValue.serverTimestamp(),
});