title: How do getters and setters change properties in Dart?
body: <p>I am struggling with the concept of getters and setters in Dart, and the more I read, the more I cannot grasp the underlying purpose.  Take for example the following code:</p>
  
  <pre class="lang-dart prettyprint-override"><code>main() {
      Car car = new Car();
      car.doors = 44;
      print(car.doors);  // 44
  }
  class Car {
      int doors = 4;
  }
  </code></pre>
  
  <p>Later, I decide to make “doors” a private variable, so I do the following:</p>
  
  <pre class="lang-dart prettyprint-override"><code>main() {
      Car car = new Car();
      car.doors = 44;
      print(car.doors);  // 44
  }
  class Car {
      int _doors = 4;
      int get doors =&gt; _doors;
      set doors(int numberOfDoors) =&gt; _doors = numberOfDoors;
  }
  </code></pre>
  
  <p>According to the code, <code>_doors</code> is now a private variable, and so I cannot access it in main().  However, by manipulating <code>doors</code>, I can indirectly change the value of <code>_doors</code>, which is what I thought I wanted to prevent in the first place by making it a private variable.  So what is the purpose of making a previously public variable into a private one, if you can still indirectly manipulate it?  And, how are getters and setters even working to change the properties of these variables? I am trying to understand the fundamental concept, because without that, I don't understand how or why getters and setters are used.</p>