Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dart

I am writing this class, and noticed the anon function design you can use for small things.

String get fullName => "$fname $lname";   //A

and I got thinking on whether i should do my setters and getters in this fashion or the standard:

void setFullName( String fn, String ln ) {   //B
  fname = fn; 
  lname = ln; 
}

String getFullName(){   //C
  return "$fname $lname"; 
}

I know that the difference between A and C is that one is a method while the other is not.

print(fullName);
// vs
print(getFullName());

So, which approach is best. I was reading a demo that said anon functions should really only be used when the function has reason to change. I personally think that getters should be functions in teh traditional sense, but some things are pretty simple to just do shorthand.

If there is a shorthand for a getter like how i wrote A, is there a shorthand for a setter?

like image 642
Fallenreaper Avatar asked Jan 15 '16 18:01

Fallenreaper


People also ask

How do you use get and set in darts?

Getter Method in Dart It is used to retrieve a particular class field and save it in a variable. All classes have a default getter method but it can be overridden explicitly. The getter method can be defined using the get keyword as: return_type get field_name{ ... }

What is the role of this keyword in DART?

This keyword in dart is used to remove the ambiguity that can be caused if the class attributes and the parameters have the same name. This keyword basically represents an implicit object pointing to the current class object.


2 Answers

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
like image 98
Günter Zöchbauer Avatar answered Nov 16 '22 01:11

Günter Zöchbauer


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

like image 35
Tokiniaina Eddy Andriamiandris Avatar answered Nov 16 '22 01:11

Tokiniaina Eddy Andriamiandris