setRoutePath method

Future<void> setRoutePath(
  1. RoutePath<RouteArguments> routePath
)

Sets the active RoutePath and builds the corresponding screen widget.

This method handles route rewrites, clears the dirty state, and updates loading states.

Implementation

Future<void> setRoutePath(RoutePath routePath) async {
  GetIt.I<DirtyStateMonitor>().clearDirty();
  if (_isInitialising) {
    _routePath = routePath;
    _currentScreen = initialScreen();
  } else {
    isLoading = true;
    // Detemine if a redirect is indicated
    RoutePath? currentPath = routePath;
    _rewrittenRoutePath = null;
    while (currentPath != null) {
      var result = await currentPath.rewrite(context);
      if (result == null) {
        _routePath = currentPath;
      } else {
        _rewrittenRoutePath = result;
      }
      currentPath = result;
    }

    if (featureAccessController != null && _routePath.isPublic == false) {
      if (featureAccessController!.currentDetails?.userId == null) {
        _routePath = rootPaths.signInRoute!();
      } else {
        if (featureAccessController!.hasPermission(_routePath.path) ==
            false) {
          _routePath = rootPaths.accessDeniedRoute!();
        }
      }
    }

    if (_routePath.isViewModel) {
      // ignore: use_build_context_synchronously
      _currentScreen = _routePath.buildViewModel(context);
    } else {
      // ignore: use_build_context_synchronously
      _currentScreen = await _routePath.build(context);
    }
    // TO-DO: Reimplement
    // Attempt to build the current route path into a screen
    // try {
    // _currentScreen = await _routePath.build(context);
    // } on ApiException catch (e) {
    //   if (e.code == 404) {
    //     _currentScreen = rootPaths.notFoundScreen();
    //   } else {
    //     rethrow;
    //   }
    // }
    isLoading = false;
  }
}