如何从提供商处保留数据状态



我只想在按下Apply按钮时更新我的数据,但提供者不允许。当我更新复选框值时,我的所有提供者数据都在更新。我如何避免这种

过滤网

https://gist.github.com/MehmetAtabey/bc7ab87dec3c42f4d46fbd8f9d9f07c9

如果我完全理解你的问题,你应该在子过滤器中添加一个Filter的副本,而不是同一个元素。因为Dart默认使用对象引用,所以您引用的是同一个对象。

为您创建一个";克隆";方法:

class Filter {
final String filterName;
final String filterGroupName;
int count = 0;
bool isSelected = false;
Filter(this.filterGroupName, this.filterName);

factory Filter.clone(Filter source){
Filter f = Filter(source.filterGroupName, source.filterName);
f.count = source.count;
f.isSelected = source.isSelected;
return f;
}
}

当您创建子列表数组时,使用";克隆";将对象添加到新列表的方法:

List<Filter> temp = widget.filters.where((element) => element.filterGroupName == widget.filterGroupName).toList();
widget.subFilters = List<Filter>();    
temp.forEach((el)=>widget.subFilters.add(Filter.clone(el)));

最新更新