Reputation: 361
What does ._()..
mean in the following code?
class CounterState {
int counter;
CounterState._();
factory CounterState.init() {
return CounterState._()..counter = 0;
}
}
More precisely - what do the two dots ..
mean past ._()
?
Upvotes: 11
Views: 3779
Reputation: 1028
According to dart documentation, your dart app is a library so even if you create a private constructor for a class, it is accessible in the whole application as it is considered as the library. So, it is not like the other OOP languages.
if you just want to prevent instantiation of class the read this article
Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore (_), it’s private to its library. For details, see Libraries and visibility.
According to dart documentation
The import and library directives can help you create a modular and shareable code base. Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore (_) are visible only inside the library. Every Dart app is a library, even if it doesn’t use a library directive.
..(property) is used to initialize or change the values of the properties.
According to me, the best use case for this would be that if you are creating an object and you want to initialize other properties that you can not initialize through the constructor there you can use it.
For example,
var obj = TestClass(prop1: "value of prop 1", prop2: "value of prop 1")
..prop3="value of prop 3";
// which improves the readability of the code with lesser code and less effort.
Upvotes: 1
Reputation: 7990
..
This called cascade-notation
Cascades (..) allow you to make a sequence of operations on the same object.
In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.
Example
querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
Upvotes: 4
Reputation: 1969
CounterState._();
is the named constructor for the class.and ..
is called cascade notation.Means we are creating the Object through the Constructor then we are setting the counter
as 0.
Upvotes: 3
Reputation: 126734
In the Dart Languague tour, which you should really check out because it contains a lot of useful information, you can also find information about the cascade notation you mentioned:
Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.
As an example, if you want to update multiple fields of your render object, you can simply use the cascade notation to save some characters:
renderObject..color = Colors.blue
..position = Offset(x, y)
..text = greetingText
..listenable = animation;
// The above is the same as the following:
renderObject.color = Colors.blue;
renderObject.position = Offset(x, y);
renderObject.text = greetingText;
renderObject.listenable = animation;
It also helps when you want to return an object in the same line as assigning a value or calling a function:
canvas.drawPaint(Paint()..color = Colors.amberAccent);
._()
is a named private constructor. If a class does not specify another constructor (either default or named) that is not private, that class cannot be instantiated from outside of the library.
class Foo {
Foo._(); // Cannot be called from outside of this file.
// Foo(); <- If this was added, the class could be instantiated, even if the private named constructor exists.
}
Learn more about private constructors.
Upvotes: 6