Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created November 4, 2023 17:02
dart singletons
// fancy constructor way
class SingletonOne {
SingletonOne._privateConstructor();
static final SingletonOne _instance = SingletonOne._privateConstructor();
factory SingletonOne() {
return _instance;
}
}
// static field with getter
class SingletonTwo {
SingletonTwo._privateConstructor();
static final SingletonTwo _instance = SingletonTwo._privateConstructor();
static SingletonTwo get instance => _instance;
}
// static field
class SingletonThree {
SingletonThree._privateConstructor();
static final SingletonThree instance = SingletonThree._privateConstructor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment