'The difference between the use of constructor " className() and className._()

I have these two ways to write a constructor. className() and className._()

What is the difference between them and when should I use which?

 class GlobalState{

      final Map<dynamic,dynamic> _data=<dynamic,dynamic>{};
      static GlobalState instance = new GlobalState._();

      GlobalState._();
    }
    //In Main Class 
       GlobalState _store=GlobalState.instance;

    and 

    class GlobalState{

      final Map<dynamic,dynamic> _data=<dynamic,dynamic>{};
      static GlobalState instance = new GlobalState();
    }
    //In Main Class 
       GlobalState _store=GlobalState();


Solution 1:[1]

Consider, for example:

class MyClass {
  MyClass();

  MyClass.named();

  MyClass._private();

  MyClass._();
}

The above class has four constructors:

  • MyClass(): This is the default, unnamed constructor. When you're designing the class, you'd want this to signify that it's the constructor that people should use most of the time. Using it is more convenient because it involves less typing.
  • MyClass.named(): This is a named constructor. Maybe you want your class to have multiple constructors so that an instance of your class can be constructed in different ways. Dart doesn't support overloaded functions, so if you want multiple constructors, they need different names. Hence, if you want multiple constructors, you'll need to name some of them to distinguish between them. Even if you don't have multiple constructors, in some cases you might want an explicit name anyway to avoid ambiguity about positional arguments it takes (e.g. Rectangle.fromLTRB).
  • MyClass._private(): In Dart, non-local names that start with an underscore are private to the Dart library (which usually means the .dart file). MyClass._private() is just a named constructor that can't be used outside of the library. Common uses are when the class:
    • Wants to force callers to use redirecting constructors that redirect to a less ergonomic private constructor.
    • Is not meant to be instantiated directly, such as if the class wants to force callers to use a public factory constructor (or static method) to get instances, particularly if the class wants to have a single instance or if it wants to disallow subclassing.
    • Is not meant to be instantiated at all.
  • MyClass._(): There's nothing special about this; it's the same as MyClass._private() but with a different name. It's not a default constructor, and it's still a private, named constructor (i.e., callers must use MyClass._() to invoke it). It's a case where the class author wanted a private constructor but didn't feel like choosing a name. (Naming things is hard.)

Solution 2:[2]

You know In java there no facility like named constructor. but dart provide named constructor. whenever you want to create more 2 constructor choice then you should create named constructor in class. like You have done in your code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Hardik Kumbhani