get method

Future<DtoChatItem?> get(
  1. ChatItemId itemId
)

Returns the stored or fetched DtoChatItem identified by the provided itemId.

Implementation

Future<DtoChatItem?> get(ChatItemId itemId) async {
  Log.debug('get($itemId)', '$runtimeType($id)');

  DtoChatItem? item = _pagination?.items[itemId];
  item ??=
      fragments
          .firstWhereOrNull((e) => e.pagination?.items[itemId] != null)
          ?.pagination
          ?.items[itemId];
  item ??= await _driftItems.read(itemId);

  if (item == null) {
    try {
      item = await _chatRepository.message(itemId);
    } catch (_) {
      // No-op.
    }

    if (item != null) {
      // Don't await [put] here, as [get] should react as quick as possible.
      //
      // Also this may cause deadlocks during fetching around a [ChatItem] in
      // [CommonDriftProvider].
      put(item);
    }
  }

  return item;
}