Singleton in Flutter

Alvaro Armijos
3 min readNov 4, 2022

--

In a simple way, singleton ensures that a Class has only one instance in all application and provides a global access point.

An instance or object of a Class is a concrete and specific representation of a Class

When to use it?

This pattern is used when we need only one instance of something in our application. For example, in the case of a user login in our application, we need this information in several parts, but it would be too expensive in terms of processing to have the user information for each flow. In this case we can create a user singleton and it would be the same instance for all flows.

Benefits

  • Controlled access to a single instance of data. That is, you can have strict control over how and when clients access it.
  • The instance is created only when it is to be used.
  • Accessible from any part, singleton is an improvement over the global variables

How do we guarantee that a Class has only one instance?

One solution is to make the same Class responsible for its only instance. The Class can guarantee that no other instance can be created and must provide a way to access the instance.

Singleton in Flutter

One way to create a singleton in Dart is using factory constructors. You can find the original proposal here. As an example we are going to create a user singleton that will have the username. For this, first we create a normal Class called UserSingleton. We make it a singleton by creating a private variable _instance of type UserSingleton. Then we create the factory UserSingleton() that returns the instance.

Finally, we create the constructor UserSingleton._internal() wich is called exactly once, and since it is private it can only be called in this Class and we also prevent it from being instantiated outside of here. This constructor can be used to initialize logic.

In the example that we are going to use, we also have a method to update the username.

Finally, the UserSingleton is as follows:

class UserSingleton {  static final UserSingleton _instance = UserSingleton._internal();  String userName = 'Alvaro';  // using a factory is important  // because it promises to return _an_ object of this type  // but it doesn't promise to make a new one.  factory UserSingleton() {    return _instance;  }  // This named constructor is the "real" constructor  // It'll be called exactly once, by the static property assignment         above  // it's also private, so it can only be called in this class  UserSingleton._internal() {    // initialization logic  }  // rest of class as normal, for example:  void updateUserName(String name) {    userName = name;  }}

For test our singleton, we are going to use a screen, where we create two userSingleton

final s1 = UserSingleton();final s2 = UserSingleton();

And with the buttons we are going to update the username.

s1.updateUserName('Eduardo');s2.updateUserName('Alvaro');

So if we update s1 with the name “Eduardo”, we see that it is updated in s1 and s2. And if we update s2 with the name “Alvaro”, we see that it is also updated in s1. This is the advantage of using a singleton, we have a single instance of the user throughout our application, we can access it from anywhere and it gives us global access, in this case to the username.

Finally what you were waiting for, here you can find the complete example:

More Design Patterns in Flutter:

If you like it, you can Buy Me A Coffee!

--

--

Alvaro Armijos

Electronic and Telecommunications Engineer | #Flutter Developer 💙 | Always eager to learn | https://www.linkedin.com/in/alvaro-armijos-sarango/