getRawData method
Retrieves raw data for a specified tag.
This method fetches and decodes the raw data associated with the given
tag from SharedPreferences. The data is expected to be stored as a
JSON string. You can optionally specify a dataTag to extract a specific
value from the decoded data map.
Parameters:
tag: AStringrepresenting the key under which the data is stored.dataTag: (Optional) AStringrepresenting the key within the JSON object to retrieve. Defaults to'data'.
Returns:
- A
Future<dynamic>that resolves to the decoded data if available, ornullif the data is not found or an error occurs during decoding.
Throws:
- Any errors during data retrieval or decoding are caught and logged,
and
nullis returned instead of propagating the exception.
Implementation
Future<dynamic> getRawData(
final String tag, {
final String dataTag = 'data',
}) async {
final SharedPreferences prefs = await _prefs;
final String? jsonString = prefs.getString('raw_$tag');
if (jsonString == null) {
return null;
}
try {
final Map<String, dynamic> decoded =
jsonDecode(jsonString) as Map<String, dynamic>;
return decoded[dataTag];
} catch (e) {
debugPrint('Error loading raw data for tag $tag: $e');
return null;
}
}