getItem<T extends StorableModel> method

Future<T?> getItem<T extends StorableModel>(
  1. String tag,
  2. String id,
  3. T fromJson(
    1. Map<String, dynamic>
    )
)

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 of T.

Returns:

  • A Future that resolves to the item of type T if found, otherwise null.

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>);
}