Instead of using the Singleton pattern, it is of course also possible, but less convenient, to define static variables or functions with the addition 'static':
staticStringgetname=>'My Flutter App';
I would say the opposite: that a static variable is by far the simpler route. It does not require the programming of a single-instance object factory constructor--nor the "creation" of this new GlobalVariables() on each retrieval, which may ultimately look tacky and less readable anyway. In fact, in your singleton solution, you employ the static concept already: in the assignment of your singleton _instance to the private constructor.
I usually opt to create an abstract class with a single constructor that is private (such that a default, nameless, no-parameter constructor is not automatically generated by Dart, especially in the case of dartdoc documentation). This class has static const fields accessible from anywhere the abstract class is imported by simply calling GlobalVariables.desiredVariable.
As a further profit from this proposed solution, these variables are constant, as per const. If utilized in an application, the value of the field is stored in memory just once and retrieved when needed. This is directly in opposition to a getter as per your String get name => 'My Flutter App' solution.
classGlobalVariables{constGlobalVariables._();// Override default constructor with a private onefactoryGlobalVariables()=>_instance;// Employ factory to redirect creationstaticconstGlobalVariables_instance=GlobalVariables._();// Create singleStringgetname=>'My Flutter App';// Define name getter}abstractclassGlobalVariablesToo{GlobalVariablesToo._();// Avoid doc generating default constructorstaticconstname='My Flutter App';// Define name constant}// Does not compile! The constructor being called isn't a const constructor.constname=GlobalVariables().name;// Works just fine. Very readable. Easy to implement. :)constnameToo=GlobalVariablesToo.name;
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would say the opposite: that a static variable is by far the simpler route. It does not require the programming of a single-instance object factory constructor--nor the "creation" of this
new GlobalVariables()on each retrieval, which may ultimately look tacky and less readable anyway. In fact, in your singleton solution, you employ thestaticconcept already: in the assignment of your singleton_instanceto the private constructor.I usually opt to create an abstract class with a single constructor that is private (such that a default, nameless, no-parameter constructor is not automatically generated by Dart, especially in the case of
dartdocdocumentation). This class hasstatic constfields accessible from anywhere the abstract class is imported by simply callingGlobalVariables.desiredVariable.As a further profit from this proposed solution, these variables are constant, as per
const. If utilized in an application, the value of the field is stored in memory just once and retrieved when needed. This is directly in opposition to a getter as per yourString get name => 'My Flutter App'solution.