toInitial method

Future<void> toInitial({
  1. Set<Type>? keep,
  2. String? id,
})

Hard-reset the app to its initial route, clearing everything.

Discards the entire navigation stack, disposes all non-permanent controllers, and reloads initialRoute from scratch — as if the app was just opened.

keep — Optional set of types to preserve during the reset. Controllers in this set won't be deleted, useful for auth services or global state that must survive the reset.

// Full hard reset (default):
Sint.toInitial();

// Keep AuthController alive during reset:
Sint.toInitial(keep: {AuthController});

Implementation

Future<void> toInitial({
  Set<Type>? keep,
  String? id,
}) async {
  final delegate = searchDelegate(id);
  final initialRoute = rootController.config.initialRoute
      ?? delegate.registeredRoutes.firstOrNull?.name
      ?? '/';

  // Hard reset: delete all non-permanent dependencies except [keep]
  if (keep != null && keep.isNotEmpty) {
    final keys = InjectionExtension.registeredKeys.toList();
    for (final key in keys) {
      final shouldKeep = keep.any((type) => key.startsWith(type.toString()));
      if (!shouldKeep) {
        delete(key: key, force: false);
      }
    }
  } else {
    deleteAll(force: false);
  }

  // Navigate to initial route, forcing a full reload
  offAllNamed(initialRoute, id: id);
}