Files
oott/frontend/lib/utils/pref_utils.dart
T
rzuastiandClaude Sonnet 4.6 e70676a193 Surface persistence and fetch errors instead of swallowing them
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>
2026-05-31 18:43:46 -04:00

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;
}
}
}