Was watching a tutorial and saw instructor defining the constructor of theme file like this
MyTheme._();
I expect to know why he didn't declare a normal constructor and if the the other constructor mean something is it a must ?
Identifiers that start with an underscore (_) are visible only inside the library. See Dart libraries.
The underscore indicates that for example a class, constructor, method, variable, getter, etc is library-private and can only be accessed from within the library where it is defined.
In the example below, A._()
is a private constructor, _a
is a private instance variable, and _B
is a private class.
class A{
/// Default constructor
A(this._a);
/// Private constructor
A._(): _a = 89;
/// Named factory constructor
factory A.named({required int a}){
if (a > 100){
return A._();
}else{
return A(a);
}
}
/// Public getter
int get a => _a;
/// Private instance variable
int _a;
/// Public getter
bool get isEven => a.isEven;
}
/// Private class
class _B extends A{
_B(): super._();
}
So the purpose of naming the constructor MyTheme._()
is to make it private.