Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 415 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a class method named ._() function in dart?

#1
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);

...
}

Reply

#2
`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.
Reply

#3
It is to make the class non-instantiable.

More on

[To see links please register here]

.


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

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through