2026-03-02 13:59:50 -05:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
|
|
class PrefUtil {
|
|
|
|
|
static late final SharedPreferences preferences;
|
|
|
|
|
static bool _init = false;
|
|
|
|
|
static Future init() async {
|
|
|
|
|
if (_init) return;
|
|
|
|
|
preferences = await SharedPreferences.getInstance();
|
|
|
|
|
_init = true;
|
|
|
|
|
return preferences;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void setValue(String key, Object value) {
|
|
|
|
|
switch (value.runtimeType) {
|
|
|
|
|
case String:
|
|
|
|
|
preferences.setString(key, value as String);
|
|
|
|
|
break;
|
|
|
|
|
case bool:
|
|
|
|
|
preferences.setBool(key, value as bool);
|
|
|
|
|
break;
|
|
|
|
|
case int:
|
|
|
|
|
preferences.setInt(key, value as int);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Object getValue(String key, Object defaultValue) {
|
|
|
|
|
switch (defaultValue.runtimeType) {
|
|
|
|
|
case String:
|
2026-03-02 19:41:01 -05:00
|
|
|
return preferences.getString(key) ?? defaultValue;
|
2026-03-02 13:59:50 -05:00
|
|
|
case bool:
|
2026-03-02 19:41:01 -05:00
|
|
|
return preferences.getBool(key) ?? defaultValue;
|
2026-03-02 13:59:50 -05:00
|
|
|
case int:
|
2026-03-02 19:41:01 -05:00
|
|
|
return preferences.getInt(key) ?? defaultValue;
|
2026-03-02 13:59:50 -05:00
|
|
|
default:
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|