Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getter and a regular method in Dart

I'm just getting my feet wet with the BLoC architecture of Flutter. Here, I wish to create a Bloc class, which will help me to convert the user input in the form of stream data. To do that task, let's say that first of all I create an instance of Bloc class with the name email and then,

  1. make use of snippet 0 & then call email.emailController.sink.add(some_string)
  2. or, make use of snippet 1 and then call email.streamEmail(some_string)
  3. or, make use of snippet 2 code and then call email.streamEmail(some_string)

to add a string input to the stream

Code snippets:

//Snippet 0 : w/o any `method`
class Bloc{
 final emailController = StreamController<String>();
}
//Snippet 1 : using regular 'method'
class Bloc{
 final emailController = StreamController<String>();
 void streamEmail(String value) => emailController.sink.add(value);
}
//Snippet 2 : using 'get' based 'method'
class Bloc{
 final emailController = StreamController<String>();
 Function(String) get streamEmail => emailController.sink.add;
}

I learned that making use of snippet 1 or 2 is a rather better to approach, in terms of code readability. I know that snippet 1& 2 are just 2 different ways of doing the same thing. However, I'm not clear about the differences that snippet 2 brings in, by making use of the getter method.

From A tour of the Dart language,

Getters and setters are special methods that provide read and write access to an object’s properties.

At the moment, the only thing I understand about getters is that they represent an alternative approach to define methods within a class. So, to be precise my questions are :

  1. Does usage of a getter method lead to enhancement or drop in-app. performance?
  2. When & why should I be using snippet 2 type class definitions instead of snippet 1?
like image 356
Argon Avatar asked Feb 16 '20 15:02

Argon


People also ask

What is a getter in dart?

The class methods, getter and setter , are used to manipulate the data of the class attributes. A setter is used to set the data of the fieldName class to some variable, whereas a getter is used to read or get the data of the fieldName class.

What are getter and setter methods in dart?

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.

Are getters and setters necessary in dart?

Prerequisites. Getters and setters are special methods that provide read and write access to an object's properties. Each instance variable of your class has an implicit getter, and a setter if needed. In dart, you can take this even further by implementing your own getters and setters.

What is the difference between getter and setter?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.


1 Answers

Does usage of a getter method lead enhancement or drop in app. performance?

No, using getter/setter instead of methods does not impact performance.

When & why should I be using snippet 2 type class definitions instead of snippet 1?

When to use getters/setters is a question about taste and some developers are more likely to use them than others. I guess a general design goal should be that getters/setters acts like normal properties on an object and should therefore not make any additional unknown behavior than getting/setting a property. (e.g. get a property of a objects will end up saving some files to the file system).

In your example I would go with snippet 1 (and maybe with a different name of the method) since your example is not really a good use case of using properties. Snippet 2 seems like a forced clever attempt to make use a getter which ends up being a little weird since the getter ends up returning a Function.

But again, this is a question about taste and I am sure there are some developers which would go with snippet 2.

like image 164
julemand101 Avatar answered Sep 16 '22 20:09

julemand101