setRawData method
Stores raw data for a specified tag.
This method encodes the provided data into a JSON string and stores it
in SharedPreferences under a key prefixed with raw_. It supports
storing primitive types or custom structures. If a corresponding stream
controller exists, it will notify listeners with the updated data.
Parameters:
tag: AStringrepresenting the key under which the data is stored.data: Adynamicvalue representing the raw data to store. This can include primitive types (e.g.,int,double,String,bool) or custom structures that are JSON-serializable.
Returns:
- A
Future<void>that completes when the data is successfully stored.
Throws:
- Any errors encountered while encoding or storing the data will propagate.
Implementation
Future<void> setRawData(final String tag, final dynamic data) async {
final SharedPreferences prefs = await _prefs;
final String jsonString = jsonEncode(<String, dynamic>{'data': data});
await prefs.setString('raw_$tag', jsonString);
// Notify if any controllers exist
if (_controllers.containsKey('raw_$tag')) {
_controllers['raw_$tag']!.add(<String, dynamic>{'data': data});
}
}