Skip to content

Instantly share code, notes, and snippets.

@iapicca
Created October 6, 2019 15:44
Show Gist options
  • Save iapicca/c465a3254c80ef7311493a76b24b643f to your computer and use it in GitHub Desktop.
Save iapicca/c465a3254c80ef7311493a76b24b643f to your computer and use it in GitHub Desktop.
Simple Singleton
class Singleton {
int _counter;
static final Singleton _instance = Singleton._internal();
factory Singleton()=> _instance;
Singleton._internal() {
_counter = 0;
}
void counterUp() => _counter +=1;
String get counter => _counter.toString();
}
void main() {
Singleton _first = Singleton();
Singleton _second = Singleton();
for(int i=0;i < 5;i++){
_first.counterUp();
print(_second.counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment