「Flutter/dart/singleton」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Singletonクラス) |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 行1: | 行1: | ||
| + | Singletonクラス | ||
<pre> | <pre> | ||
class SingletonHoge { | class SingletonHoge { | ||
| − | + | String name = ""; | |
| − | static final SingletonHoge | + | static final SingletonHoge _singleton = SingletonHoge._internal(); |
| − | + | factory SingletonHoge() { | |
| − | return | + | return _singleton; |
| + | } | ||
| + | SingletonHoge._internal() { | ||
| + | // init | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||
| + | 呼び出しサンプル | ||
<pre> | <pre> | ||
| − | SingletonHoge hoge = SingletonHoge. | + | SingletonHoge hoge = SingletonHoge(); |
| + | 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 | ||
2020年1月28日 (火) 18:56時点における最新版
Singletonクラス
class SingletonHoge {
String name = "";
static final SingletonHoge _singleton = SingletonHoge._internal();
factory SingletonHoge() {
return _singleton;
}
SingletonHoge._internal() {
// init
}
}
呼び出しサンプル
SingletonHoge hoge = SingletonHoge(); print (hoge.name); // "" hoge.name = "taro"; print (hoge.name); // "taro"
https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart
