Last active
November 24, 2020 09:36
-
-
Save malinkaphann/2357c22807bf36e5b931d5da1f6d2ddf to your computer and use it in GitHub Desktop.
how to create singleton in dart
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
/** | |
* author: malinkaphann@gmail.com | |
* how to create singleton class | |
*/ | |
// singleton class | |
class MySingleton { | |
MySingleton._privateConstructor(); | |
static final MySingleton instance = MySingleton._privateConstructor(); | |
} | |
// how to call it | |
void main() { | |
//MySingleton one = MySingleton(); | |
MySingleton one = MySingleton.instance; | |
MySingleton two = MySingleton.instance; | |
// validate if the two objects about is really the same | |
print(one == two); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment