flutter - 过滤 Firestore 数据库
问题描述
在应用程序内部,我有 4 个不同的选项卡,我想显示来自 firestore 的过滤数据。
我有以下标签:
- 全部
- 音乐
- 健康
- 喜剧
初始选项卡显示所有未过滤的数据,第二个选项卡显示具有标签“音乐”的数据,依此类推。
我将代码复制了 4 次并更改了 .where 但这个解决方案对我来说似乎不是最好的。
这是我的其中一个选项卡的代码:
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Container(
width: MediaQuery.of(context).size.width,
height: 150.0,
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('eventsdata')
.orderBy('dateform')
.where('dateform', isGreaterThanOrEqualTo: _timeminus1)
.where('platform', isEqualTo: 'Comedy')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text(
'Loading...',
style:
TextStyle(fontSize: 15.0, color: Colors.black38),
);
default:
return Padding(
padding: const EdgeInsets.only(bottom: 179.0),
child: new ListView(
有人能帮我吗?
解决方案
如果我正确理解了您的问题,那么您的初始选项卡会显示所有未过滤的数据,那么我将保存此查询中的数据并将其用于其他屏幕。如果您已经在设备上拥有所有数据,那么过滤设备上的数据比对 firebase 进行多次查询更有意义,因为您按读/写收费。所以我会将我的数据保存为List<MyClass>
然后将其映射到小部件以显示数据。
如何在颤动中过滤列表?
遍历列表以呈现widegets
这些不是教程,但它们应该为您指明正确的方向,它们是我能找到的最佳材料
ListView.builder(
itemCount: myInfo.length,
itemBuilder: (context, index) => InfoCard(myInfo[index]),
),
myInfo 是您的列表,InfoCard 是一个可重用的小部件,如果您不知道如何制作可重用的小部件,如果您不需要它可以像文本小部件一样简单,我附上了一个链接。
推荐阅读
- c# - 使用asp.net c#获取带有alt键代码符号的网页源代码
- html - HTML - check if content will fit into fix-sized div
- powershell - 如何获取 azure function app 凭据?
- php - date_format() 期望参数 1 为 DateTimeInterface,给定 null (查看:/home/vagrant/code/blog/resources/views/singlePost.blade.php)
- python - Python heroku 为socket.io 聊天应用程序配置procfile gunicorn + gevent | RuntimeError: 您需要使用 gevent-websocket 服务器
- javascript - 如何在允许听众工作的同时将 HTML 附加到 div?
- java - 查找数组元素的索引
- csv - 将字符串更改为仅一行 i csv
- android - Android MySQL 数据库 - ListView [打开活动]
- bash - 使用 sed 或 awk 过滤特定模式?