route method
Generate a URL for a named route.
Implementation
String route(
String name, {
Map<String, String>? parameters,
Map<String, dynamic>? query,
}) {
final routePath = _namedRoutes[name];
if (routePath == null) {
throw ArgumentError('Route "$name" is not defined');
}
var url = routePath;
// Replace route parameters
if (parameters != null) {
for (final entry in parameters.entries) {
url = url.replaceAll(':${entry.key}', entry.value);
}
}
// Check for missing parameters
final missingParams = RegExp(r':(\w+)').allMatches(url);
if (missingParams.isNotEmpty) {
final paramNames = missingParams.map((m) => m.group(1)!).toList();
throw ArgumentError(
'Missing required parameters for route "$name": $paramNames',
);
}
return this.url(url, query: query);
}