Object-Oriented Programming: What are Singletons?

Exploring Singletons in Object-Oriented Programming with a Practical Flutter Code Example

Filip Doganowski
Tech Blog
4 min readMar 22, 2023

--

Photo by Alexander Grigorian from Pexels

What is a Singleton?

A Singleton is an object-oriented programming (OOP) design pattern that guarantees a class will have only one instance and allows for a single global entry point to access it. This is crucial in situations where having multiple instances of a class can lead to unexpected behaviors or inconsistencies in an application.

For instance, having multiple instances of a class that represents local storage can result in data being out of sync.
Similarly, an operating system must have a single file system to avoid conflicts between multiple file systems.

The singleton pattern is also commonly used to manage shared resources or configuration settings that should only have one instance throughout the app’s lifetime.

When to Use a Singleton in Flutter?

Photo by Tiana from Pexels

Singleton is a useful design pattern in Flutter when you need to manage the state of your app. It can help you create an object that persists throughout the app’s lifecycle, allowing different parts of the app to access and modify the same state. Here are some situations where you may want to consider using a singleton in your Flutter app:

  1. Managing app configuration settings: If your app has configuration settings that should be accessible from anywhere in the app, you can use a singleton to manage these settings. For example, you may want to store API keys or default settings that should persist throughout the app’s lifetime.
  2. Managing user authentication state: If your app requires user authentication, you can use a singleton to manage the user’s authentication state. This allows different parts of the app to check whether the user is authenticated without having to pass the user’s authentication state around as arguments.
  3. Managing global app state: If your app has state that needs to be accessed and modified by different parts of the app, you can use a singleton to manage this state. For example, you may want to store a list of items that should be accessible from different screens in the app.

Code Snippet Examples

Photo by Mike B from Pexels

Example 1: Managing app configuration settings

class AppConfig {
static final AppConfig _singleton = AppConfig._internal();

String apiKey;

factory AppConfig() {
return _singleton;
}

AppConfig._internal();
}

In this example, we create a singleton class called AppConfig. This class has a private constructor that can only be called from within the class. We also create a static final instance of the class called _singleton. This instance is only created once and globally accessible through the factory method.

Example 2: Managing user authentication state

class AuthState {
static final AuthState _singleton = AuthState._internal();

bool isAuthenticated = false;

factory AuthState() {
return _singleton;
}

AuthState._internal();
}

In this example, we create a singleton class called AuthState to manage the user’s authentication state. The isAuthenticated property stores whether the user is authenticated or not. The class has a private constructor that can only be called from within the class and a static final instance of the class called _singleton.

Example 3: Managing global app state

class AppState {
static final AppState _singleton = AppState._internal();

List<String> items = [];

factory AppState() {
return _singleton;
}

AppState._internal();
}

In this example, we create a singleton class called AppState to manage global app state. The items property is used to store a list of items that should be accessible from different parts of the app. The class has a private constructor that can only be called from within the class, and a static

Conclusion

Singletons are a powerful design pattern in object-oriented programming that allows for the creation of a single instance of a class. They are particularly useful when multiple instances of a class could cause unexpected behavior or inconsistencies in an application. In Flutter, singletons are commonly used to manage app configuration settings, user authentication, and global app states. The code examples provided demonstrate how to create and use singletons in Flutter. By leveraging singletons, developers can create more efficient and maintainable code to improve the overall user experience.

If you enjoyed the read give me some claps 👏 !

Have any thoughts? Something, you would like to share? Leave a response!

--

--

Filip Doganowski
Tech Blog

Passionate Flutter Developer, Project Manager and Content Creator, visit my website: https://flutterdog.com