简体   繁体   English

如何使用 Dart/Flutter 正确过滤列表

[英]How to properly filter a List with Dart/Flutter

I'm struggling to find a way to properly filter a list with dart/flutter without losing its content.我正在努力寻找一种方法来使用 dart/flutter 正确过滤列表而不丢失其内容。

For context:对于上下文:

  • I have a list with 100 items.我有一个包含 100 个项目的列表。
  • I use this list in a ListView我在 ListView 中使用这个列表
  • I have a button to filter the list using the Where().ToList()我有一个按钮可以使用 Where().ToList() 过滤列表
  • Once filtered, the original list get reduced to the filtered value and I can't recover the initial (100 values)过滤后,原始列表会减少到过滤后的值,我无法恢复初始值(100 个值)

My understand (or lack of) is that even if I create a new object copying the list, still references the original, so the original gets deleted.我的理解(或缺乏)是,即使我创建一个新的 object 复制列表,仍然引用原件,所以原件被删除。 So I am not sure how can I use filters without losing data?所以我不确定如何在不丢失数据的情况下使用过滤器?

The filter call:过滤器调用:

 onTap: (int index) {
        switch (index) {
          case 0:
            widget.collection!.items =
                widget.collection!.filterCollection(FilterType.all);
            _selectedIndex = index;
            break;
          case 1:
            widget.collection!.items =
                widget.collection!.filterCollection(FilterType.onlyFew);
            _selectedIndex = index;
            break;
        }

On the filterCollection (which is a method inside the class that hold the items) I added a secondary list (filteredList where I tried to keep the original list so I could revert back to it, but doesnt work either)在 filterCollection(这是 class 中保存项目的方法)上,我添加了一个辅助列表(filteredList,我试图保留原始列表,以便我可以恢复到它,但也不起作用)

filterCollection(FilterType filter) {
    filteredList.clear();
    filteredList.addAll(items); //items is a List<Items> part of this class
    switch (filter) {
      case FilterType.onlyFew:
        return filteredList
            .where((element) => element.something == true)
            .toList();
      case FilterType.all:
      default:
        return filteredList;
    }

My latest guess (which I will try now) is to try to use a "copy" of the list in the widget.collection so I don't change the original?我最近的猜测(我现在会尝试)是尝试在 widget.collection 中使用列表的“副本”,这样我就不会更改原始列表? I don't know, completely lost:D我不知道,完全迷失了:D

Any suggestion?有什么建议吗?

You should have a list with all items and a second list with the filtered items.您应该有一个包含所有项目的列表和一个包含过滤项目的第二个列表。

Default the filteredList should have the initial list with all items.默认 filteredList 应具有包含所有项目的初始列表。 Your ListView only works with the filteredList.您的 ListView 仅适用于 filteredList。

Inside your onTap set your filteredList:在你的 onTap 里面设置你的 filteredList:

switch (index) {
          case 0:
            filteredList =
                widget.collection!.filterCollection(FilterType.all);
            _selectedIndex = index;
            break;
          case 1:
            filteredList =
                widget.collection!.filterCollection(FilterType.onlyFew);
            _selectedIndex = index;
            break;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM