首页 > 解决方案 > 过滤 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(

有人能帮我吗?

标签: fluttergoogle-cloud-firestorecloud

解决方案


如果我正确理解了您的问题,那么您的初始选项卡会显示所有未过滤的数据,那么我将保存此查询中的数据并将其用于其他屏幕。如果您已经在设备上拥有所有数据,那么过滤设备上的数据比对 firebase 进行多次查询更有意义,因为您按读/写收费。所以我会将我的数据保存为List<MyClass>然后将其映射到小部件以显示数据。

如何在颤动中过滤列表?
遍历列表以呈现widegets

这些不是教程,但它们应该为您指明正确的方向,它们是我能找到的最佳材料

ListView.builder(
        itemCount: myInfo.length,
        itemBuilder: (context, index) => InfoCard(myInfo[index]),
      ),

myInfo 是您的列表,InfoCard 是一个可重用的小部件,如果您不知道如何制作可重用的小部件,如果您不需要它可以像文本小部件一样简单,我附上了一个链接。

可重用的自定义小部件

Flutter:小型可重用小部件的强大功能


推荐阅读