54 lines
1.3 KiB
Dart
54 lines
1.3 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
enum StorageServiceValueType {
|
|
string,
|
|
stringList,
|
|
boolean,
|
|
}
|
|
|
|
class StorageService {
|
|
|
|
StorageService._();
|
|
|
|
static late SharedPreferences _preferences;
|
|
|
|
static Future<void> init() async {
|
|
_preferences = await SharedPreferences.getInstance();
|
|
}
|
|
|
|
static T? getValue<T>( StorageServiceValueType valueType, String key ){
|
|
|
|
Map<StorageServiceValueType, Function( String key ) > data = {
|
|
StorageServiceValueType.string: _preferences.getString,
|
|
StorageServiceValueType.stringList: _preferences.getStringList,
|
|
StorageServiceValueType.boolean: _preferences.getBool,
|
|
};
|
|
|
|
final Function( String key )? function = data[valueType];
|
|
|
|
if( function == null ) throw Exception('Unimplemented value type');
|
|
|
|
return function(key) as T?;
|
|
|
|
}
|
|
|
|
static Future<bool> setValue( StorageServiceValueType valueType, String key, dynamic value ) async {
|
|
|
|
switch( valueType ) {
|
|
case .string:
|
|
return _preferences.setString(key, value);
|
|
case.stringList:
|
|
return _preferences.setStringList(key, value);
|
|
case .boolean:
|
|
return _preferences.setBool(key, value);
|
|
}
|
|
|
|
throw Exception('Unimplemented value type');
|
|
|
|
}
|
|
|
|
static Future<bool> removeValue( String key ) async {
|
|
return await _preferences.remove(key);
|
|
}
|
|
|
|
} |