Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

singleton in dart

class FileSystemManager {
  static final FileSystemManager _instance = FileSystemManager._internal();
 
  // 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 FileSystemManager() {
    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
  FileSystemManager._internal() {
    // initialization logic 
  }
  
  // rest of class as normal, for example:
  void openFile() {}
  void writeFile() {}
}

Source by flutterbyexample.com #
 
PREVIOUS NEXT
Tagged: #singleton #dart
ADD COMMENT
Topic
Name
8+8 =