getInternalState<T> method
Obtiene estado interno
Implementation
Future<T?> getInternalState<T>(String key) async {
ObslyLogger.debug('π ObslyStorage.getInternalState<$T>() called - key: $key, initialized: $_isInitialized');
if (!_isInitialized) {
ObslyLogger.verbose('π Storage not initialized, calling initialize()...');
await initialize();
}
try {
ObslyLogger.debug(
'π Storage state: useHive=$_useHive, internalStateBox=${_internalStateBox != null}, prefs=${_prefs != null}');
if (_useHive && _internalStateBox != null) {
ObslyLogger.verbose('π Reading from Hive box...');
final rawResult = _internalStateBox!.get(key);
ObslyLogger.debug(
'π Hive raw result for "$key": ${rawResult?.runtimeType} - ${rawResult != null ? rawResult.toString() : 'NULL'}');
if (rawResult == null) {
return null;
}
// Manejar conversiΓ³n de tipos para Maps (preservado de HEAD)
if (rawResult is Map && T.toString().contains('Map<String, dynamic>')) {
ObslyLogger.debug('π Converting Map to Map<String, dynamic> for "$key"');
return Map<String, dynamic>.from(rawResult) as T;
}
// Safe type conversion (de develop)
try {
if (rawResult is T) {
ObslyLogger.debug('π Direct type match for "$key"');
return rawResult;
} else {
ObslyLogger.debug('π Type mismatch for "$key": expected $T, got ${rawResult.runtimeType}');
return null;
}
} catch (e) {
ObslyLogger.error('π Type conversion error for "$key": $e');
return null;
}
} else if (_prefs != null) {
ObslyLogger.verbose('π Reading from SharedPreferences...');
final typeString = T.toString(); // Necesario para el manejo de Maps mΓ‘s abajo
if (T == String) {
final result = _prefs!.getString('obsly_state_$key') as T?;
ObslyLogger.debug(
'π SharedPreferences string result for "$key": ${result != null ? 'FOUND (${result.toString().length} chars)' : 'NULL'}');
return result;
} else if (T == int) {
try {
// DEFENSIVE: Check actual stored type to prevent crashes from corrupted data
final anyValue = _prefs!.get('obsly_state_$key');
if (anyValue == null) {
ObslyLogger.debug('π SharedPreferences int result for "$key": NULL');
return null;
}
if (anyValue is int) {
ObslyLogger.debug('π SharedPreferences int result for "$key": $anyValue');
return anyValue as T;
}
// Corrupted data - log and return null
ObslyLogger.error('β Corrupted data for key "$key": expected int, found ${anyValue.runtimeType}');
return null;
} catch (e) {
ObslyLogger.error('β Error getting internal state for key "$key": $e');
return null;
}
} else if (T == bool) {
try {
// DEFENSIVE: Check actual stored type to prevent crashes from corrupted data
final anyValue = _prefs!.get('obsly_state_$key');
if (anyValue == null) {
ObslyLogger.debug('π SharedPreferences bool result for "$key": NULL');
return null;
}
if (anyValue is bool) {
ObslyLogger.debug('π SharedPreferences bool result for "$key": $anyValue');
return anyValue as T;
}
// Corrupted data - log and return null
ObslyLogger.error('β Corrupted data for key "$key": expected bool, found ${anyValue.runtimeType}');
return null;
} catch (e) {
ObslyLogger.error('β Error getting internal state for key "$key": $e');
return null;
}
} else {
final json = _prefs!.getString('obsly_state_$key');
ObslyLogger.debug(
'π SharedPreferences JSON for "$key": ${json != null ? 'FOUND (${json.length} chars)' : 'NULL'}');
if (json != null) {
final decoded = jsonDecode(json);
// Manejar el caso especΓfico de Map<String, dynamic> tambiΓ©n para SharedPreferences
if (typeString.contains('Map<String, dynamic>') && decoded is Map) {
return Map<String, dynamic>.from(decoded) as T;
}
return decoded as T?;
}
}
} else {
ObslyLogger.error('β No storage backend available for reading!');
}
ObslyLogger.debug('π Returning null for key "$key"');
return null;
} catch (e) {
ObslyLogger.error('β Error getting internal state for key "$key": $e');
return null;
}
}