setNewRoutePath method
Called by the Router when the Router.routeInformationProvider reports that a new route has been pushed to the application by the operating system.
Consider using a SynchronousFuture if the result can be computed synchronously, so that the Router does not need to wait for the next microtask to schedule a build.
Implementation
@override
Future<void> setNewRoutePath(RouteInformation configuration) {
return _serializeNavigation(() async {
final location = configuration.uri.toString();
if (location == _location) {
return;
}
// 浏览器后退/前进:目标页面已存在于当前栈中,直接回退到该页面。
final existingIndex = _pages.lastIndexWhere((entry) {
final entryName = entry.materialPage.name;
return entryName == location || entryName == configuration.uri.path;
});
if (existingIndex != -1) {
for (var index = _pages.length - 1; index > existingIndex; index--) {
final removedPage = _pages.removeAt(index);
_result.remove(removedPage.id);
_pendingPopResults.remove(removedPage.id);
}
_location = location;
notify();
return;
}
//
final resolvedPage =
_resolvePage(configuration) ?? _notFoundPage ?? const EmptyPage();
if (!await _checkGuards(
to: configuration,
toPageType: resolvedPage.runtimeType,
)) {
return;
}
// 初始 URL 或栈中只有根页面时,替换而不是叠加。
if (_pages.length <= 1) {
_clearPageStack();
}
await _pushEntry(page: resolvedPage, name: location);
});
}