getRawData method

Future getRawData(
  1. String tag, {
  2. String dataTag = 'data',
})

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: A String representing the key under which the data is stored.
  • dataTag: (Optional) A String representing the key within the JSON object to retrieve. Defaults to 'data'.

Returns:

  • A Future<dynamic> that resolves to the decoded data if available, or null if the data is not found or an error occurs during decoding.

Throws:

  • Any errors during data retrieval or decoding are caught and logged, and null is 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;
  }
}