DEV Community

Cover image for Implementing Singleton Pattern In Dart - Flutter
Bibek
Bibek

Posted on • Edited on • Originally published at blog.bibekkakati.me

4 3

Implementing Singleton Pattern In Dart - Flutter

What is a singleton pattern?

Singleton pattern is a design pattern that allows us to use a single instance of a class everywhere.

Implementation

class ClassName {
    static ClassName _className;

    ClassName._createInstance();
    factory ClassName() {
        if (_className == null) {
            _className = ClassName._createInstance();
        }
       return _className;
    }
}
Enter fullscreen mode Exit fullscreen mode

Factory constructors return an instance of the class, but it doesn't necessarily create a new instance.

Thank you for reading. Give it a thumbs-up if it is helpful for you.

Feel free to connect 👋


Originally published on blog.bibekkakati.me


Sentry blog image

The Visual Studio App Center’s retiring

But sadly….you’re not. See how to make the switch to Sentry for all your crash reporting needs.

Read more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay