zero_coding zero_coding -4 years ago 302
Dart Question

Use getter and setter

Is is recommended to use in dart getter and setter to define a property like:

class Car {
Engine engine;

bool get isEngineRunning => engine.isRunning;

void set isEngineRunning(bool isRunning) {
engine.isRunning = isRunning;

And what is advantage of it?

Answer Source

Only if it's required because you want to execute some additional code except forwarding to a field.

If the getters and setters are only used to wrap a field, the getters/setters are explicitly discouraged because they are just redundant.

In your case, because you are not forwarding to a field _isEngineRunning it's fine if you don't want to expose Engine engine.

However because engine is public it might be just too much noise and confusing because the same thing can be done in two different ways.

If engine would be private it would be better considering

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download