Flutter 工具类 SpUtil
2018-10-31 本文已影响4人
Sky24n
我的 : Flutter开源库集合
GitHub: SpUtil
Pub : SpUtil
学习Flutter的都知道,Flutter的SharedPreferences不能像Android里面一样直接getSharedPreferences得到,而是Future<SharedPreferences> getInstance() async异步获取。
网上很多都是这样
static getString(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(key);
}
SpUtil同步获取字符串
static String getString(String key) {
if (_prefs == null) return null;
return _prefs.getString(key);
}
SpUtil使用单例模式,使用synchronized Lock实现。仅在App启动需要异步等待,在SpUtil.getInstance()完成后,可同步获取SpUtil.getString("xxx").
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
loadAsync();
}
void loadAsync() async {
await SpUtil.getInstance(); //等待Sp初始化完成
SpUtil.putString("username", "sky224");
String username = SpUtil.getString("username");
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Center(
child: new Text(' '),
),
floatingActionButton: FloatingActionButton(onPressed: () {
String username = SpUtil.getString("username"); //同步获取
}),
),
);
}
}
SpUtil已经开源到Flutter常用工具类库flustars,欢迎Star,以下是SpUtil源码:
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:synchronized/synchronized.dart';
/**
* @Author: thl
* @GitHub: https://github.com/Sky24n
* @Flutter常用工具类: https://github.com/Sky24n/flustars
* @Description: Sp Util.
* @Date: 2018/9/8
*/
/// SharedPreferences Util.
class SpUtil {
static SpUtil _singleton;
static SharedPreferences _prefs;
static Lock _lock = Lock();
static Future<SpUtil> getInstance() async {
if (_singleton == null) {
await _lock.synchronized(() async {
if (_singleton == null) {
// 保持本地实例直到完全初始化。
var singleton = SpUtil._();
await singleton._init();
_singleton = singleton;
}
});
}
return _singleton;
}
SpUtil._();
Future _init() async {
_prefs = await SharedPreferences.getInstance();
}
static String getString(String key) {
if (_prefs == null) return null;
return _prefs.getString(key);
}
static Future<bool> putString(String key, String value) {
if (_prefs == null) return null;
return _prefs.setString(key, value);
}
static bool getBool(String key) {
if (_prefs == null) return null;
return _prefs.getBool(key);
}
static Future<bool> putBool(String key, bool value) {
if (_prefs == null) return null;
return _prefs.setBool(key, value);
}
static int getInt(String key) {
if (_prefs == null) return null;
return _prefs.getInt(key);
}
static Future<bool> putInt(String key, int value) {
if (_prefs == null) return null;
return _prefs.setInt(key, value);
}
static double getDouble(String key) {
if (_prefs == null) return null;
return _prefs.getDouble(key);
}
static Future<bool> putDouble(String key, double value) {
if (_prefs == null) return null;
return _prefs.setDouble(key, value);
}
static List<String> getStringList(String key) {
return _prefs.getStringList(key);
}
static Future<bool> putStringList(String key, List<String> value) {
if (_prefs == null) return null;
return _prefs.setStringList(key, value);
}
static dynamic getDynamic(String key) {
if (_prefs == null) return null;
return _prefs.get(key);
}
static Set<String> getKeys() {
if (_prefs == null) return null;
return _prefs.getKeys();
}
static Future<bool> remove(String key) {
if (_prefs == null) return null;
return _prefs.remove(key);
}
static Future<bool> clear() {
if (_prefs == null) return null;
return _prefs.clear();
}
///Sp is initialized.
static bool isInitialized() {
return _prefs != null;
}
}