Bläddra i källkod

version 0.1.2

tanghongliang 7 år sedan
förälder
incheckning
149aeff058
5 ändrade filer med 49 tillägg och 35 borttagningar
  1. 1 1
      CHANGELOG.md
  2. 1 0
      README.md
  3. 2 0
      example/lib/main.dart
  4. 44 33
      lib/src/sp_util.dart
  5. 1 1
      pubspec.yaml

+ 1 - 1
CHANGELOG.md

@@ -1,4 +1,4 @@
-## 0.1.0
+## 0.1.2
 
 * TODO: SpUtil updated.
 

+ 1 - 0
README.md

@@ -49,6 +49,7 @@ getDynamic
 getKeys
 remove
 clear
+isInitialized
 ```
 
 * #### ScreenUtil

+ 2 - 0
example/lib/main.dart

@@ -16,8 +16,10 @@ class _MyAppState extends State<MyApp> {
   }
 
   void test() async {
+    print("SpUtil: " + SpUtil.isInitialized().toString());
     SpUtil spUtil = await SpUtil.getInstance();
     //SpUtil.remove("username");
+    print("SpUtil: " + SpUtil.isInitialized().toString());
     SpUtil.putString("username", "sky224");
     print("username: " + SpUtil.getString("username").toString());
   }

+ 44 - 33
lib/src/sp_util.dart

@@ -10,92 +10,103 @@ import 'package:synchronized/synchronized.dart';
  * @Date: 2018/9/8
  */
 
-///
+/// SharedPreferences Util.
 class SpUtil {
   static SpUtil _singleton;
-  static SharedPreferences _SP;
+  static SharedPreferences _prefs;
   static Lock _lock = Lock();
 
   static Future<SpUtil> getInstance() async {
     if (_singleton == null) {
       await _lock.synchronized(() async {
-        _singleton = new SpUtil();
-        await init();
+        if (_singleton == null) {
+          // keep local instance till it is fully initialized.
+          var singleton = SpUtil._();
+          await singleton._init();
+          _singleton = singleton;
+        }
       });
     }
     return _singleton;
   }
 
-  static void init() async {
-    _SP = await SharedPreferences.getInstance();
+  SpUtil._();
+
+  Future _init() async {
+    _prefs = await SharedPreferences.getInstance();
   }
 
   static String getString(String key) {
-    if (_SP == null) return null;
-    return _SP.getString(key);
+    if (_prefs == null) return null;
+    return _prefs.getString(key);
   }
 
   static Future<bool> putString(String key, String value) {
-    if (_SP == null) return null;
-    return _SP.setString(key, value);
+    if (_prefs == null) return null;
+    return _prefs.setString(key, value);
   }
 
   static bool getBool(String key) {
-    if (_SP == null) return null;
-    return _SP.getBool(key);
+    if (_prefs == null) return null;
+    return _prefs.getBool(key);
   }
 
   static Future<bool> putBool(String key, bool value) {
-    if (_SP == null) return null;
-    return _SP.setBool(key, value);
+    if (_prefs == null) return null;
+    return _prefs.setBool(key, value);
   }
 
   static int getInt(String key) {
-    if (_SP == null) return null;
-    return _SP.getInt(key);
+    if (_prefs == null) return null;
+    return _prefs.getInt(key);
   }
 
   static Future<bool> putInt(String key, int value) {
-    if (_SP == null) return null;
-    return _SP.setInt(key, value);
+    if (_prefs == null) return null;
+    return _prefs.setInt(key, value);
   }
 
   static double getDouble(String key) {
-    if (_SP == null) return null;
-    return _SP.getDouble(key);
+    if (_prefs == null) return null;
+    return _prefs.getDouble(key);
   }
 
   static Future<bool> putDouble(String key, double value) {
-    if (_SP == null) return null;
-    return _SP.setDouble(key, value);
+    if (_prefs == null) return null;
+    return _prefs.setDouble(key, value);
   }
 
   static List<String> getStringList(String key) {
-    return _SP.getStringList(key);
+    return _prefs.getStringList(key);
   }
 
   static Future<bool> putStringList(String key, List<String> value) {
-    if (_SP == null) return null;
-    return _SP.setStringList(key, value);
+    if (_prefs == null) return null;
+    return _prefs.setStringList(key, value);
   }
 
   static dynamic getDynamic(String key) {
-    if (_SP == null) return null;
-    return _SP.get(key);
+    if (_prefs == null) return null;
+    return _prefs.get(key);
   }
 
   static Set<String> getKeys() {
-    if (_SP == null) return null;
-    return _SP.getKeys();
+    if (_prefs == null) return null;
+    return _prefs.getKeys();
   }
 
   static Future<bool> remove(String key) {
-    if (_SP == null) return null;
-    return _SP.remove(key);
+    if (_prefs == null) return null;
+    return _prefs.remove(key);
   }
 
   static Future<bool> clear() {
-    if (_SP == null) return null;
-    return _SP.clear();
+    if (_prefs == null) return null;
+    return _prefs.clear();
+  }
+
+  ///Sp is initialized.
+  static bool isInitialized() {
+    return _prefs != null;
   }
 }

+ 1 - 1
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flustars
 description: Flutter common utils library. SpUtil, ScreenUtil, WidgetUtil.
-version: 0.1.1
+version: 0.1.2
 author: thl <863764940@qq.com>
 homepage: https://github.com/Sky24n/flustars