Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active July 25, 2020 00:52
Show Gist options
  • Save hectorAguero/53636c9a5a578cfe1067aa812fca55dc to your computer and use it in GitHub Desktop.
Save hectorAguero/53636c9a5a578cfe1067aa812fca55dc to your computer and use it in GitHub Desktop.
Dart Singleton Example
// Way 1 from here
//https://stackoverflow.com/questions/54057958/comparing-ways-to-create-singletons-in-dart
class Log1 {
factory Log1() => _instance;
Log1._privateConstructor(){
print('Hello Singleton');
}
static final Log1 _instance = Log1._privateConstructor();
}
class Log2 {
Log2(){
print('Hello Normal Class');
}
}
void main() {
for (int i = 0; i < 5; i++) {
Log1();
Log2();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment