DEV Community

Cover image for 🦋Flutter - Access global non-changing variables
Luciano Jung
Luciano Jung

Posted on • Updated on

🦋Flutter - Access global non-changing variables

In Flutter, you often have non-changing values that are used across the app (for example the app name, version, etc.) Wouldn't it be nice for you to have globally managed access for these?
Of course and I'll show you how to do it here:

The basic idea

For the simple administration of global values it is sufficient to have a static access to a class, which provides the needed values. For this it is recommended to use the Singleton pattern in this class.

The solution

To solve this problem we generate a class (I call it Globalvariables for this example). In this we implement the singleton pattern as shown below.

class GlobalVariables{

  static final GlobalVariables _instance = GlobalVariables._internal();

  factory GlobalVariables() => _instance;

  GlobalVariables._internal();

  [...]

}
Enter fullscreen mode Exit fullscreen mode

Then we create the getter functions without declaring variables.

class GlobalVariables{

  [...]

  String get name => 'My Flutter App';
  String get version => '1.0.0';
  int get versionCode => 100;
}
Enter fullscreen mode Exit fullscreen mode

We can now access these Variables with the following line of code:

GlobalVariables().name // => My Flutter App
Enter fullscreen mode Exit fullscreen mode

Comments

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':

static String get name => 'My Flutter App';
Enter fullscreen mode Exit fullscreen mode

If you have any comments or questions, feel free to leave a comment.

Feel free to follow my profile if you want to learn more practical tips about Flutter.
Also feel free to visit my Github profile to learn more about my current OpenSource projects.

Top comments (1)

Collapse
 
zabadam profile image
Zabadam

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':

static String get name => '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.

class GlobalVariables {
  const GlobalVariables._(); // Override default constructor with a private one
  factory GlobalVariables() => _instance; // Employ factory to redirect creation
  static const GlobalVariables _instance = GlobalVariables._(); // Create single
  String get name => 'My Flutter App'; // Define name getter
}

abstract class GlobalVariablesToo {
  GlobalVariablesToo._(); // Avoid doc generating default constructor
  static const name = 'My Flutter App'; // Define name constant
}

// Does not compile! The constructor being called isn't a const constructor.
const name = GlobalVariables().name;

// Works just fine. Very readable. Easy to implement. :)
const nameToo = GlobalVariablesToo.name;
Enter fullscreen mode Exit fullscreen mode