setCurrentWebPageIndex method

void setCurrentWebPageIndex(
  1. RouterServiceInterface service
)

Sets current index using current Route on Web Platform.

Allows to get the index from url during a browser refresh which means the app starts from scracth.

Implementation

void setCurrentWebPageIndex(RouterServiceInterface service) {
  if (!service.topRoute.hasPendingChildren) return;

  int? currentWebPageIndex;

  try {
    currentWebPageIndex = service.router.routes
        .firstWhere(
          (route) => route.name == service.topRoute.name,
        )
        .children
        ?.routes
        .skipWhile((route) => route.name.startsWith('#'))
        .toList()
        .indexWhere(
          (route) =>
              route.name == service.topRoute.pendingChildren.first.name,
        );
  } on StateError catch (_) {
    /// The route does not meet the condition in the firstWhere call
  } catch (_) {
    /// Catch everything else
  }

  if (currentWebPageIndex == null || currentWebPageIndex == -1) return;

  setIndex(currentWebPageIndex);
}