flutter singleton

142

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.
main() {
  var s1 = Singleton();
  var s2 = Singleton();
  print(identical(s1, s2));  // true
  print(s1 == s2);           // true
}

Comments

Submit
0 Comments