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 {
        return jsonDecode(value) as T;
      } catch (_) {
        return value as T;
      }
    }

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