watchRawData method
Watches raw data for a specified tag.
This method provides a stream that emits updates to the raw data
associated with the given tag. It fetches the initial data from
SharedPreferences and listens for further changes via the stream
controller. You can optionally specify a dataTag to extract a specific
value from the emitted data.
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<Stream<dynamic>>that emits the raw data as it is updated.
Throws:
- Any errors during data retrieval or stream operations are propagated.
Implementation
Future<Stream<dynamic>> watchRawData(
final String tag, {
final String dataTag = 'data',
}) async {
final StreamController<Map<String, dynamic>> controller = _getController(
'raw_$tag',
);
// Initial load
await getRawData(tag).then((final dynamic data) {
if (data != null) {
controller.add(<String, dynamic>{dataTag: data});
}
});
return controller.stream.map(
(final Map<String, dynamic> data) => data[dataTag],
);
}