replaceAll method
Replace all existing routes with new routes with push animation
Note: If integrate with GoRouter, please call removeAll() and then
use go() to set new routes instead of using this method.
Implementation
void replaceAll(List<NavRouteInfo> routeInfos) {
try {
final context = _currentContext;
if (context == null) {
if (_enableLogger) {
debugPrint('NavService.replaceAll: No valid context found.');
}
return;
}
if (routeInfos.isEmpty) {
if (_enableLogger) {
debugPrint('NavService.replaceAll: No routeInfos provided.');
}
return;
}
final navigator = Navigator.of(context);
final currentIndex = _steps.length - 1;
if (currentIndex > 0) {
// Remove all existing routes
for (final step in List<NavStep>.from(_steps.reversed)) {
if (step.currentRoute.isActive) {
navigator.removeRoute(step.currentRoute);
}
}
// Push new routes
pushAll(routeInfos);
} else {
// handle when there is no existing _steps
// and contains initial route
// Push first route with or without animation
final firstRouteInfo = routeInfos.first;
final firstRoute = _routes[firstRouteInfo.path];
if (firstRoute == null) {
if (_enableLogger) {
debugPrint(
'NavService.replaceAll: Route not found for path: '
'${firstRouteInfo.path}',
);
}
} else {
navigator.pushAndRemoveUntil(
_buildPageRouteNoPushAnimation(
path: firstRouteInfo.path,
extra: NavExtra(firstRouteInfo.extra ?? {}),
route: firstRoute,
),
(route) => false,
);
}
// Push remaining routes in order with animation at last
final remainingRouteInfos = routeInfos.sublist(1);
pushAll(remainingRouteInfos);
}
// ignore: avoid_catches_without_on_clauses
} catch (e, st) {
if (_enableLogger) {
debugPrint('NavService.replaceAll.exception: $e\n$st');
}
}
}