Last active
July 25, 2020 00:52
-
-
Save hectorAguero/53636c9a5a578cfe1067aa812fca55dc to your computer and use it in GitHub Desktop.
Dart Singleton Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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