1

I'm still getting used to Dart and I've searched for an answer to this, but I haven't found one. What does the second line, 'ClassA._();', of the class definition do? The list declaration is only shown to give the class a reason for being.

class ClassA {   
   ClassA._();   
   static final List<String> someList = ["A", "B", "C"]; 
}

If it's a constructor, how would it be invoked? After doing some more sleuthing I see it creates a singleton. But when is the singleton created? Is there a way to get in front of the instantiation and make some mods?

Thanks in advance for any help given.

1 Answers1

1
ClassA._();

Is a named constructor with the name _. In dart, identifiers that start with underscore (_) are only visible inside the library it is contained in.

The reason you would define a class this way, is that you want to prevent people from creating instances of this class.

mmcdon20
  • 5,767
  • 1
  • 18
  • 26
  • mmcdon20, thank you for your response. That makes sense. The syntax is so terse. – Mike Yinger Dec 03 '21 at 16:24
  • Dart doesn't have keywords like `private` or `protected` and instead uses leading underscores in identifiers to control visibility. It is a bit unusual compared to other languages, so its something to keep in mind when reading dart code. – mmcdon20 Dec 03 '21 at 16:32