facebook twitter hatena line email

「Flutter/dart/singleton」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「<pre> class SingletonHoge { SingletonHoge._privateConstructor(); static final SingletonHoge _instance = SingletonHoge._privateConstructor(); static SingletonHoge ge...」)
 
(同じ利用者による、間の3版が非表示)
行1: 行1:
 
<pre>
 
<pre>
 
class SingletonHoge {
 
class SingletonHoge {
   SingletonHoge._privateConstructor();
+
   String name = "";
   static final SingletonHoge _instance = SingletonHoge._privateConstructor();
+
   static final SingletonHoge _singleton = SingletonHoge._internal();
   static SingletonHoge get instance {
+
   factory SingletonHoge() {
     return _instance;
+
     return _singleton;
 +
  }
 +
  SingletonHoge._internal() {
 +
    // init
 
   }
 
   }
 
}
 
}
 +
 +
</pre>
 +
<pre>
 +
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

2019年12月2日 (月) 22:25時点における版

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