「Flutter/dart/singleton」の版間の差分
提供: 初心者エンジニアの簡易メモ
行1: | 行1: | ||
<pre> | <pre> | ||
class SingletonHoge { | class SingletonHoge { | ||
+ | String name = ""; | ||
SingletonHoge._privateConstructor(); | SingletonHoge._privateConstructor(); | ||
static final SingletonHoge _instance = SingletonHoge._privateConstructor(); | static final SingletonHoge _instance = SingletonHoge._privateConstructor(); | ||
行10: | 行11: | ||
<pre> | <pre> | ||
SingletonHoge hoge = SingletonHoge.instance; | SingletonHoge hoge = SingletonHoge.instance; | ||
+ | print (hoge.name); // "" | ||
+ | hoge.name = "taro"; | ||
+ | print (hoge.name); // "taro" | ||
</pre> | </pre> | ||
https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart | https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart |
2019年12月2日 (月) 21:42時点における版
class SingletonHoge { String name = ""; SingletonHoge._privateConstructor(); static final SingletonHoge _instance = SingletonHoge._privateConstructor(); static SingletonHoge get instance { return _instance; } }
SingletonHoge hoge = SingletonHoge.instance; print (hoge.name); // "" hoge.name = "taro"; print (hoge.name); // "taro"
https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart