removeAll method

void removeAll()

Required call before call GoRouter's context.go()

Remove all routes without animation

Caution: If use NavService standalone without GoRouter, DON'T call this method because it can lead to error _history.isEmpty.

Insteads use removeUntil

Implementation

void removeAll() {
  try {
    final context = _currentContext;
    if (context == null) {
      if (_enableLogger) {
        debugPrint('NavService.removeAll: No valid context found.');
      }
      return;
    }

    if (_steps.isEmpty) {
      if (_enableLogger) {
        debugPrint('NavService.removeAll: No steps to remove.');
      }
      return;
    }

    final navigator = Navigator.of(context);

    // Create a copy of steps to avoid ConcurrentModificationError
    final stepsToRemove = List<NavStep>.from(_steps.reversed);

    // Remove routes without animation in reverse order using routes
    // from steps
    for (final step in stepsToRemove) {
      if (step.currentRoute.isActive) {
        navigator.removeRoute(step.currentRoute);
      }
    }

    // Clear internal navigation history will be handled in _didRemove
    // ignore: avoid_catches_without_on_clauses
  } catch (e, st) {
    if (_enableLogger) {
      debugPrint('NavService.removeAll.exception: $e\n$st');
    }
  }
}