initNearByVideos method

dynamic initNearByVideos(
  1. int index
)

Implementation

initNearByVideos(int index) async {
  if (init) {
    lastIndex = index;
    return;
  }
  lastIndex = null;
  init = true;
  if (loading.value) return;
  disposeNearByOldVideoControllers(index);
  await tryInit(index);
  try {
    var currentPage = index;
    var maxPage = currentPage + loadLimit;
    List<String> videoFiles = videoList;

    for (var i = currentPage; i < maxPage; i++) {
      if (videoFiles.asMap().containsKey(i)) {
        var controller = videoPlayerControllerList[i];
        if (!controller.value.isInitialized) {
          cacheVideo(i);
          await controller.initialize();
          increasePage(i + 1);
          refreshView();
          // listenEvents(i);
        }
      }
    }
    for (var i = index - 1; i > index - loadLimit; i--) {
      if (videoList.asMap().containsKey(i)) {
        var controller = videoPlayerControllerList[i];
        if (!controller.value.isInitialized) {
          cacheVideo(index);
          await controller.initialize();
          increasePage(i + 1);
          refreshView();
          // listenEvents(i);
        }
      }
    }

    refreshView();
    loading.value = false;
  } catch (e) {
    loading.value = false;
  } finally {
    loading.value = false;
  }
  init = false;
}