load method

Future<GiphyGif> load(
  1. int index
)

Loads the element with the given index

Implementation

Future<GiphyGif> load(int index) async {
  final existing = _cache[index];
  if (existing != null) {
    return existing;
  }
  if (!hasElementAt(index)) {
    final total = nextCursor != null ? nextCursor : totalCount;
    throw IndexError.withLength(
      index,
      _cache.length,
      message: 'Invalid index $index in source with total $total',
    );
  }
  final page = index ~/ _pageLength;
  var pageRequest = _requestedPages[page];
  if (pageRequest == null) {
    pageRequest = _loadPage(page);
    _requestedPages[page] = pageRequest;
  }
  await pageRequest;
  final loaded = _cache[index];
  if (loaded == null) {
    throw GiphyClientError(400, 'Unable to load item $index in source');
  }
  return loaded;
}