Getters and setters are mandatory for others classes to access instance variables of a class to not violate the encapsulation principle (which is quite important in OO programming). In the end it’s a matter of choice, but you’re saving one method call using your first example.
Read moreAre getters and setters necessary dart?
Using getter/setter are optionals . It could be helpful in some situations , for example when you need to add additional logic when you get the data.
Read moreWhy do we use getters and setters in darts?
Getter and setter methods are the class methods used to manipulate the data of the class fields. Getter is used to read or get the data of the class field whereas setter is used to set the data of the class field to some variable .31 Eki 2021
Read moreWhy getters and setters are bad?
Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access to implementation details . What if you need to change the accessed field’s type? You also have to change the accessor’s return type.
Read more