switchToFeature static method

void switchToFeature(
  1. GoRouter router,
  2. NavigationStorage storage, {
  3. required String featureKey,
  4. required String defaultPath,
  5. String? currentFeatureKey,
})

Switches to featureKey, saving the current URI under currentFeatureKey (if given) and restoring featureKey's previously saved URI, or defaultPath if it has none.

Implementation

static void switchToFeature(
  GoRouter router,
  NavigationStorage storage, {
  required String featureKey,
  required String defaultPath,
  String? currentFeatureKey,
}) {
  final currentUri = router.routerDelegate.currentConfiguration.uri;
  storage.clearAllModalCompleters();

  if (currentFeatureKey != null && currentFeatureKey != featureKey) {
    final fullUri = currentUri.toString();
    storage.featurePaths[currentFeatureKey] = fullUri;
  }

  final savedUri = storage.featurePaths[featureKey];
  final targetPath = savedUri ?? defaultPath;
  try {
    router.go(targetPath);
  } catch (e) {
    debugPrint('[NavigationUtils] Error navigating to $targetPath: $e');
    rethrow;
  }
}