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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 18:43:46 -04:00
|
|
|
static Future<bool> setValue(String key, Object value) {
|
2026-05-27 13:57:03 -04:00
|
|
|
switch (value) {
|
|
|
|
|
case String s:
|
2026-05-31 18:43:46 -04:00
|
|
|
return preferences.setString(key, s);
|
2026-05-27 13:57:03 -04:00
|
|
|
case bool b:
|
2026-05-31 18:43:46 -04:00
|
|
|
return preferences.setBool(key, b);
|
2026-05-27 13:57:03 -04:00
|
|
|
case int i:
|
2026-05-31 18:43:46 -04:00
|
|
|
return preferences.setInt(key, i);
|
2026-03-02 13:59:50 -05:00
|
|
|
default:
|
2026-05-31 18:43:46 -04:00
|
|
|
throw ArgumentError(
|
|
|
|
|
'Unsupported pref value type: ${value.runtimeType}',
|
|
|
|
|
);
|
2026-03-02 13:59:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Object getValue(String key, Object defaultValue) {
|
2026-05-27 13:57:03 -04:00
|
|
|
switch (defaultValue) {
|
|
|
|
|
case String _:
|
2026-03-02 19:41:01 -05:00
|
|
|
return preferences.getString(key) ?? defaultValue;
|
2026-05-27 13:57:03 -04:00
|
|
|
case bool _:
|
2026-03-02 19:41:01 -05:00
|
|
|
return preferences.getBool(key) ?? defaultValue;
|
2026-05-27 13:57:03 -04: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|