title: Why variable A is not being copied to variable B?
body: <p>I'm using two variables with the same value. Variable A is an initial variable and variable B is the one that I use to apply changes, so when I want to reset variable B I just assign A to it, the problem is each time I do it, the changes of B then applied to both. I have look in many places and these are the solutions I tried:</p>
  <ol>
  <li><p>List.toList()</p>
  <pre><code>_fields!.clear();
  _fields = _initFields!.toList();
  </code></pre>
  </li>
  <li><p>Spread operator (...)</p>
  <pre><code>_fields!.clear();
  _fields = [...?_initFields];
  </code></pre>
  </li>
  </ol>
  <p>Also tried the ones below from this post: <a href="https://coflutter.com/dart-flutter-how-to-clone-copy-a-list/" rel="nofollow noreferrer">Dart/Flutter – How to clone/copy a list</a></p>
  <pre><code>var newNumbers = List.from(numbers);
  var newNumbers = List.generate(numbers.length, (index) =&gt; numbers[index]);
  var newNumbers = List.of(numbers);
  var newNumbers = List.unmodifiable(numbers);
  </code></pre>
  <p>Here the complete code:</p>
  <pre><code>import 'package:project/utilities/classes/filter_field.dart';
  
  class FilterLogic {
  
    List&lt;FilterField&gt;? _initFields = [];
    List&lt;FilterField&gt;? _fields = [];
    bool? listComplete = false;
  
    void addField(FilterField? field) {
  
        _fields!.add(field!);
  
    }
  
    void addInitField(FilterField? field) {
  
      _initFields!.add(field!);
  
    }
  
    List&lt;FilterField&gt;? getFields() {
  
      return _fields;
  
    }
  
    void resetFieldsToOriginal() {
  
      _fields!.clear();
      _fields = [...?_initFields!];
  
    }
  
    void showFields() {
  
      print('_fields --------------------------------------------------------------------------------------');
  
      _fields!.forEach((element) {
        if(element.checked == true) {
          print({ element.checked, element.field, element.filteredApplied, element.criteria, element.filteringOperators});
        }
      });
  
      print('_initFields --------------------------------------------------------------------------------------');
  
      _initFields!.forEach((element) {
        if(element.checked == true) {
          print({ element.checked, element.field, element.filteredApplied, element.criteria, element.filteringOperators});
        }
      });
  
    }
  
  }
  </code></pre>
  <p>Here is how I fill both variables:</p>
  <pre><code>filterCol.forEach((column) {
    String? filteredApplied;
    String? filteredCriteria = 'None';
    bool checked = false;
  
    filtersList.forEach((element) {
      if (column.field == element.field) {
        filteredApplied = element.operator;
        filteredCriteria = element.criteria;
        checked = true;
      }
    });
  
      FilterField field = FilterField(
        filteredApplied: filteredApplied,
        field: column.field,
        label: column.label,
        filteringOperators: column.filteringOperators,
        criteria: filteredCriteria,
        checked: checked == true ? true : false,
      );
  
    FilterField initField = FilterField(
      filteredApplied: filteredApplied,
      field: column.field,
      label: column.label,
      filteringOperators: column.filteringOperators,
      criteria: filteredCriteria,
      checked: checked == true ? true : false,
    );
  
    setState(() {
  
      filterLogic.addField(field);
      filterLogic.addInitField(initField);
  
    });
    
  });
  </code></pre>
  <p>Here where I call the reset method:</p>
  <pre><code>DialogButton(
    width: 106.25,
    child: Text(
      &quot;CANCEL&quot;,
      style: TextStyle(
          color: Colors.white, fontSize: 14, fontFamily: 'Chivo'),
    ),
    onPressed: () =&gt; {
      Navigator.of(context).pop(),
      setState(() {
      filterLogic.resetFieldsToOriginal();  // &lt;-------------
    })
    },
    color: Color.fromRGBO(84, 84, 84, 1),
    radius: BorderRadius.circular(0.0),
  ),
  </code></pre>