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,
email.emailController.sink.add(some_string)
email.streamEmail(some_string)
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 :
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With