Files
GSFV2/gsf/lib/scale/utils/local_storage.dart
2024-04-10 12:51:20 +05:30

55 lines
1.1 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
///SharedPreferences 本地存储
class LocalStorage {
static late final SharedPreferences prefs;
static bool _init = false;
static Future init() async {
if (_init) return;
prefs = await SharedPreferences.getInstance();
_init = true;
return prefs;
}
static saveData<T>(String key, T value) {
switch (T) {
case String:
prefs.setString(key, value as String);
break;
case int:
prefs.setInt(key, value as int);
break;
case bool:
prefs.setBool(key, value as bool);
break;
case double:
prefs.setDouble(key, value as double);
break;
}
}
static T? getData<T>(String key) {
T? res;
switch (T) {
case String:
res = prefs.getString(key) as T?;
break;
case int:
res = prefs.getInt(key) as T?;
break;
case bool:
res = prefs.getBool(key) as T?;
break;
case double:
res = prefs.getDouble(key) as T?;
break;
}
return res;
}
}