How can I keep state of data from provider

187 Views Asked by At

I just wanna update my data when I press Apply Button, But provider does not allow it. When I update my checkbox value state all of my provider data is updating. How can I avoid this

Filter Screen

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

1

There are 1 best solutions below

0
On BEST ANSWER

If I understood well your question you should add to subfilters a copy of your Filter, not the same element. Because Dart uses by default object references, you are referencing the same object.

Create for you filter a "clone" method:

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;
  }
}

When you create the sub list array, use the "clone" method to add the object to the new list:

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)));