「Flutter/dart/singleton」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行1: | 行1: | ||
| + | ==Singletonクラス== | ||
<pre> | <pre> | ||
class SingletonHoge { | class SingletonHoge { | ||
| 行10: | 行11: | ||
} | } | ||
} | } | ||
| − | |||
</pre> | </pre> | ||
| + | 呼び出しサンプル | ||
<pre> | <pre> | ||
SingletonHoge hoge = SingletonHoge(); | SingletonHoge hoge = SingletonHoge(); | ||
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
