read<T> method

Future<T?> read<T>(
  1. String key
)

Implementation

Future<T?> read<T>(String key) async {
  final prefixedKey = '${StorageConfig.prefsPrefix}$key';

  try {
    final value = _prefs.get(prefixedKey);

    if (value == null) return null;

    if (value is List<String> && T != List<String>) {
      try {
        final decodedList = value.map((e) => jsonDecode(e)).toList();
        return decodedList as T;
      } catch (_) {
        return value as T;
      }
    }

    if (value is String && T != String) {
      try {
        final decoded = jsonDecode(value);
        // Si decoded es List y el caller espera lista, castear elementos a String
        if (decoded is List) {
          return List<String>.from(decoded) as T;
        }
        return decoded as T;
      } catch (_) {
        return value as T;
      }
    }

    return value as T;
  } catch (e) {
    debugPrint('IceStorage PrefsService: Error leyendo $key - $e');
    return null;
  }
}