Last active
May 17, 2024 05:48
-
-
Save kariyayo/16a0590ef50ad7d3d1e3ad88f97e2f21 to your computer and use it in GitHub Desktop.
This file contains 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 Color { | |
Color(this.name); | |
String name; | |
String toString() { | |
return 'Color:${this.name}'; | |
} | |
} | |
class ColorHolder { | |
Color? _cache; | |
void create() { | |
if (_cache == null) { | |
_cache = Color("black"); | |
} else { | |
_cache = Color("red"); | |
} | |
} | |
Color? get() { | |
return _cache; | |
} | |
} | |
void main() { | |
final holder = ColorHolder(); | |
final c1 = holder.get(); | |
print(c1); | |
print('\n create \n'); | |
holder.create(); | |
final c2 = holder.get(); | |
print('c1: $c1'); | |
print('c2: $c2'); | |
print('\n create \n'); | |
updateInSomeWhere(c2); | |
print('c1: $c1'); | |
print('c2: $c2'); | |
} | |
void updateInSomeWhere(Color c) { | |
if (c != null) c.name = "blue"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment