颤振在选项卡导航中从firebase检索数据的最有效方法
我想从firebase firestore接收月、周和日(默认为天)的数据 上图显示了ma选项卡Ui小部件。每个选项卡都应该提供我在颤振在选项卡导航中从firebase检索数据的最有效方法,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我想从firebase firestore接收月、周和日(默认为天)的数据 上图显示了ma选项卡Ui小部件。每个选项卡都应该提供我在FutureBuilder和ListView.Builder中向用户显示的数据 我正在寻找一种强大的方式来显示我的数据,因为没有选择周和月,我不必在第一步加载内容,而且我对firebase的请求总是相同的,只是日期在更改。我很感谢一个好的、强大的解决方案 我也不确定这是否是正确的方法,我在这里要求的数据在未来是无限的。如何处理这个问题 这是我的要求: getDat
FutureBuilder
和ListView.Builder
中向用户显示的数据
我正在寻找一种强大的方式来显示我的数据,因为没有选择周和月,我不必在第一步加载内容,而且我对firebase的请求总是相同的,只是日期在更改。我很感谢一个好的、强大的解决方案
我也不确定这是否是正确的方法,我在这里要求的数据在未来是无限的。如何处理这个问题
这是我的要求:
getData(DataNotifier dataNotifier) async {
// demo data only
List<Data> _dataList = [];
DateTime _now = DateTime.now();
DateTime _start = DateTime(_now.year, _now.month - 1, 1, 0, 0); // <- This will change
QuerySnapshot snapshot = await db
.collection('data')
.where('dateWhenProofed', isGreaterThanOrEqualTo: _start)
.limit(30)
.get()
.catchError((error) => print("---- Failed: $error"));
snapshot.docs.forEach((document) {
Data data = Data.fromJson(document.data());
_dataList.add(data);
});
dataNotifier.datalist = _dataList;
}
getData(DataNotifier DataNotifier)异步{
//仅演示数据
列表_dataList=[];
DateTime _now=DateTime.now();
DateTime _start=DateTime(_now.year,_now.month-1,1,0,0);//打印(----失败:$error”);
snapshot.docs.forEach((文档){
Data=Data.fromJson(document.Data());
_dataList.add(数据);
});
dataNotifier.datalist=\u datalist;
}
我想将我的数据显示为选项卡下方的列表。
这些是我的标签:
tabs: [
Tab(
child: Padding(
padding: const EdgeInsets.fromLTRB(
5, 0, 5, 0),
child: Text(
'Heute',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w500),
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.fromLTRB(
5, 0, 5, 0),
child: Text(
'Woche',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w500),
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.fromLTRB(
5, 0, 5, 0),
child: Text(
'Monat',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w500),
),
),
),
],
controller: _tabController,
isScrollable: true,
indicatorWeight: 0.0,
onTap: (){}, <---- we can do something here
)
选项卡:[
标签(
孩子:填充(
填充:const EdgeInsets.fromLTRB(
5, 0, 5, 0),
子:文本(
"香叶",,
样式:TextStyle(
颜色:颜色,白色,
尺寸:12,
fontWeight:fontWeight.w500),
),
),
),
标签(
孩子:填充(
填充:const EdgeInsets.fromLTRB(
5, 0, 5, 0),
子:文本(
"Woche",,
样式:TextStyle(
颜色:颜色,白色,
尺寸:12,
fontWeight:fontWeight.w500),
),
),
),
标签(
孩子:填充(
填充:const EdgeInsets.fromLTRB(
5, 0, 5, 0),
子:文本(
“莫纳特”,
样式:TextStyle(
颜色:颜色,白色,
尺寸:12,
fontWeight:fontWeight.w500),
),
),
),
],
控制器:\ tab控制器,
isScrollable:是的,
指示器重量:0.0,
onTap:(){},Firebase中有Timestamp字段,您可以使用它来计算数据是否在给定的边界中
您可以向数据添加时间戳值:
Timestamp.now()
比如说,
Timestamp get timeLimit {
final limit = DateTime.now().subtract(const Duration(days: 1));
return Timestamp.fromDate(limit);
}
您可以获得时间戳值的下限,即从查询时算起的1天
然后
这看起来不错,非常感谢!正如我在帖子中所说,我所要求的数据将是无限的。我可以这样要求吗?例如,如果用户将选项卡更改为“周”,我该怎么办?我正在将您的函数更改为给定的日期范围,但是如何使用futurebuilder呢?另外,每个选项卡总共有3个FutureBuilders,一个带有列表视图的选项卡,还是只有一个futurebuilder更改结果?提前谢谢!你说无限是什么意思?你的意思是没有时间戳边界吗?或者数据量非常大?我的意思是数据量非常大。对不起。我现在在FutureBuilder中使用一个ListView.Builder,如果用户打开一个选项卡(天、周、月),我会通过FutureBuilder的选定选项卡请求数据。对于大量的数据,现在我的问题是这是常见的方法吗?如果你使用tab controller,你必须为每个tabview创建3个不同的列表。视图,我认为这是可以的。如果您必须只使用一个list.view生成器,而不是只创建一个接收输入(例如流)的list.view,并且您可以在任何时候单击按钮,将不同的输入放入同一个流控制器。我认为最好使用时间戳对数据进行排序,并对每月数据运行一次查询。通过这种方式,您可以缓存每月数据,然后每天和每周对其进行过滤。
QuerySnapshot snapshot = await db
.collection('data')
.where('timestamp', isGreaterThanOrEqualTo: timeLimit)
.limit(30)