Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a class method named ._() function in dart?

Tags:

flutter

dart

I've seen this code can anyone please explain to me what the AppTheme._() means, as I've read about its singleton class in dart but I really can't understand how it works.

class AppTheme {   AppTheme._();    static const Color notWhite = Color(0xFFEDF0F2);   static const Color nearlyWhite = Color(0xFFFEFEFE);   static const Color white = Color(0xFFFFFFFF);   static const Color nearlyBlack = Color(0xFF213333);    ... } 
like image 602
Shadi Ossaili Avatar asked Sep 10 '19 20:09

Shadi Ossaili


People also ask

What does ._() mean in Flutter?

. _() 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.

What is _() in Dart?

_(); is a named constructor (another examples might be the copy constructor on some objects in the Flutter framework: ThemeData. copy(...); ). In dart, if the leading character is an underscore, then the function/constructor is private to the library.

WHAT IS function and method in Dart?

A function is a top-level function which is declared outside of a class or an inline function that is created inside another function or inside method. A method is tied to an instance of a class and has an implicit reference to this .


1 Answers

AppTheme._(); is a named constructor (another examples might be the copy constructor on some objects in the Flutter framework: ThemeData.copy(...);).

In dart, if the leading character is an underscore, then the function/constructor is private to the library. That's also the case here, and the underscore is also the only character, so I'd imagine whoever wrote this constructor didn't plan for that constructor to ever be called at all.

The AppTheme._(); isn't necessary unless you don't want AppTheme to ever be accidentally instantiated using the implicit default constructor.

like image 130
Sub 6 Resources Avatar answered Sep 21 '22 16:09

Sub 6 Resources