create a singleton class in flutter code example

Example 1: flutter singleton

class Singleton {
  static final Singleton _instance = Singleton._internal();

  factory Singleton() => _instance;

  Singleton._internal();
}

// Whenever you need to get the singleton, call its factory constructor, e.g.:
//   var singleton = Singleton();
//
// You'll always get the same instance.

Example 2: flutter singleton

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

  factory Singleton() {
    return _singleton;
  }

  Singleton._internal();
}

Example 3: singleton classes in dart example

class Singleton {
  static Singleton _instance;
  
  Singleton._internal();
  
  static Singleton getInstance() {
    if (_instance == null) {
      _instance = Singleton._internal();
    }
    
    return _instance;
  }
}

Tags:

Misc Example