Trying to do setters and getters in dart using the get keyword

First in Dart fields are automatically wrapped by getters and setters. If you use reflection (mirrors) to query the members of a class, you'll get getters and setters instead of fields.
This is also reflected in the Dart style guide:

Don't wrap fields with getters/setters just to be sure.

You can change fields to getters/setters any time without breaking code that uses your class.

Other things to consider for getters/setters are: they shouldn't do expensive computation and they shouldn't have side-effects (except of setting the backing field when calling the setter).

If these criteria are met (and you don't need additional parameters), then it's usually a good idea to use getters/setters.

Note: short-hand function notation is not limited to getters/setters and they also don't require it. You can use it for functions as well and you can have block bodies for getters/setters as well.

bool _isSet;
bool get isSet {
  return _isSet;
}
set isSet(bool value) {
  if(value is null) {
    throw new ArgumentError();
  }
  _isSet = value;
}

and you use them like

var myClass = new MyClass();
myClass.isSet = true; // setter
print(myClass.isSet); // getter

unnecessary_getters_setters

From the style guide:

AVOID wrapping fields in getters and setters just to be "safe".

In Java and C#, it's common to hide all fields behind getters and setters (or properties in C#), even if the implementation just forwards to the field. That way, if you ever need to do more work in those members, you can without needing to touch the callsites. This is because calling a getter method is different than accessing a field in Java, and accessing a property isn't binary-compatible with accessing a raw field in C#.

Dart doesn't have this limitation. Fields and getters/setters are completely indistinguishable. You can expose a field in a class and later wrap it in a getter and setter without having to touch any code that uses that field.

enter image description here

Tags:

Dart