【问题标题】:Filter Flutter listview.builder by category from Firestore从 Firestore 按类别过滤 Flutter listview.builder
【发布时间】:2021-08-16 17:05:56
【问题描述】:

我正在尝试按类别过滤我的列表,但到目前为止没有成功。我尝试了几种方法来实现它,还问了一些朋友。不幸的是,他们的答案都没有奏效。

这是屏幕的完整代码。我希望能够按类别过滤文档,例如:“Where()”...

有什么想法吗?

谢谢。

class ListGameWidget extends StatefulWidget {
  ListGameWidget(this._gameList, this._title);

  final List<Game> _gameList;
  final String _title;

  @override
  _ListGameWidgetState createState() => _ListGameWidgetState();
}

class _ListGameWidgetState extends State<ListGameWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: widget._gameList != null && widget._gameList.isNotEmpty
              ? Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget._title,
                      style:
                          TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                    ),
                    Expanded(
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: widget._gameList.length,
                          itemBuilder: (context, index) {
                            var game = widget._gameList[index];
                            return Card(
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10)),
                              child: Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(10),
                                      child: Image(
                                        image: NetworkImage(widget._gameList
                                            .elementAt(index)
                                            .imagen1),
                                        height: 100.0,
                                        width: 133.0,
                                      ),
                                    ),
                                  ),
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: [
                                      Text(
                                        game.tienda,
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold),
                                      ),
                                      Text(game.tipo),
                                      Text(game.comunidad),
                                      Text(game.tel1, style: TextStyle()),
                                    ],
                                  ),
                                ],
                              ),
                            );
                          }),
                    ),
                  ],
                )
              : Center(child: CircularProgressIndicator()),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 您要过滤什么?您只有一个列表。
  • 谢谢!这是商店列表,所以我想按类别(餐厅、面包店...)过滤它们
  • 我不知道您是如何加载数据的,但也许最好的方法是从 Firestore 加载过滤后的数据。
  • 可能是。谢谢!

标签: flutter filtering


【解决方案1】:

您应该在 _gameList 和 setState 中重新填充您的数据集。例如: - 为商店的字母顺序创建一个按钮 -重组数据集并在onTap事件中填充_gameList。

【讨论】:

  • 谢谢。如何在屏幕加载时显示特定类别的元素?
  • 您可以使用 initState 在屏幕加载之前预加载数据。
  • 感谢您的帮助!
【解决方案2】:

您首先需要有一个类别,它在您的代码中缺失。 您使用类别从原始列表创建列表:

String category="id of category";
List filtered = originalList.where((e)=>e.categoryId == category).toList();

In listview use the filtered list to build your elements.

但是,您的代码仍然缺少很多细节。

【讨论】:

  • 感谢您的帮助。我确实输入了完整的代码。
  • 我不确定你是认真的还是恶意的。分享您的模型,并分享您想要过滤的内容。您发布的所有代码仅显示一个列表视图构建器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
相关资源
最近更新 更多