Skip to content

Instantly share code, notes, and snippets.

@malinkaphann
Last active November 24, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malinkaphann/2357c22807bf36e5b931d5da1f6d2ddf to your computer and use it in GitHub Desktop.
Save malinkaphann/2357c22807bf36e5b931d5da1f6d2ddf to your computer and use it in GitHub Desktop.
how to create singleton in dart
/**
* 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