mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
PrefUtil.setValue now returns the underlying SharedPreferences result and throws on unsupported types; settings save reports failures rather than always showing success. Device detail and event history report the real Dio error message and skip cancellations. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
41 lines
1.1 KiB
Dart
41 lines
1.1 KiB
Dart
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 Future<bool> setValue(String key, Object value) {
|
|
switch (value) {
|
|
case String s:
|
|
return preferences.setString(key, s);
|
|
case bool b:
|
|
return preferences.setBool(key, b);
|
|
case int i:
|
|
return preferences.setInt(key, i);
|
|
default:
|
|
throw ArgumentError(
|
|
'Unsupported pref value type: ${value.runtimeType}',
|
|
);
|
|
}
|
|
}
|
|
|
|
static Object getValue(String key, Object defaultValue) {
|
|
switch (defaultValue) {
|
|
case String _:
|
|
return preferences.getString(key) ?? defaultValue;
|
|
case bool _:
|
|
return preferences.getBool(key) ?? defaultValue;
|
|
case int _:
|
|
return preferences.getInt(key) ?? defaultValue;
|
|
default:
|
|
return defaultValue;
|
|
}
|
|
}
|
|
}
|