getItem<T extends StorableModel> method
Retrieves a stored item by its id from a specific tag.
This method fetches data for the given tag from local storage. If the
specified id exists, it deserializes the item using the fromJson
function
and returns it.
Parameters:
tag: A unique identifier for the data collection.id: The unique identifier of the item to retrieve.fromJson: A function that converts a Map<String, dynamic> into an instance ofT.
Returns:
- A Future that resolves to the item of type
Tif found, otherwisenull.
Usage: Use this method to retrieve an item from local storage by its identifier.
Implementation
Future<T?> getItem<T extends StorableModel>(
final String tag,
final String id,
final T Function(Map<String, dynamic>) fromJson,
) async {
final Map<String, dynamic> data = await _loadData(tag);
final dynamic itemData = data[id];
if (itemData == null) {
return null;
}
return fromJson(itemData as Map<String, dynamic>);
}