Skip to content

Instantly share code, notes, and snippets.

@JosLuna98
Last active October 26, 2020 16:36
Show Gist options
  • Save JosLuna98/d1e4be8c42520c59145ce64b63a65e54 to your computer and use it in GitHub Desktop.
Save JosLuna98/d1e4be8c42520c59145ce64b63a65e54 to your computer and use it in GitHub Desktop.
[Dart] "Lock" singleton values
class People {
static final People _inst = People._internal();
People._internal();
factory People(int id, String name) {
assert(!_inst._lock, "it's a singleton that can't re-defined");
_inst.id = id;
_inst.name = name;
_inst._lock = true;
return _inst;
}
int id;
String name;
bool _lock = false;
}
void main() {
var people = People(0, 'Dylan');
try{
print('Instance of = ' + People(0, 'Joe').name);
print('Instance of = ' + People(1, 'Maria').name);
print('Instance of = ' + People(2, 'Ete sech').name);
} finally {
print('Instance of = ' + people.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment