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

It is to make the class non-instantiable.

More on https://www.woolha.com/tutorials/dart-prevent-instantiation-of-class#:~:text=Creating%20Private%20Constructor%20to%20Prevent,(underscore)%20which%20means%20private.

Also, I think this summarise why we need it in the first place "If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. " from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/private-constructors


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.

Tags:

Dart

Flutter