Last active
October 26, 2020 16:36
-
-
Save JosLuna98/d1e4be8c42520c59145ce64b63a65e54 to your computer and use it in GitHub Desktop.
[Dart] "Lock" singleton values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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