Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created November 4, 2023 17:02
Show Gist options
  • Save fredgrott/feaf4d232b4a8c6be5cfb7033c086f7e to your computer and use it in GitHub Desktop.
Save fredgrott/feaf4d232b4a8c6be5cfb7033c086f7e to your computer and use it in GitHub Desktop.
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