GoRoute constructor
GoRoute({
- required String path,
- String? name,
- GoRouterWidgetBuilder? builder,
- GoRouterPageBuilder? pageBuilder,
- GoRouterRedirect? redirect,
- List<
RouteBase> routes = const <RouteBase>[],
Constructs a GoRoute.
path
andname
cannot be empty strings.- One of either
builder
orpageBuilder
must be provided.
Implementation
GoRoute({
required this.path,
this.name,
this.builder,
this.pageBuilder,
this.parentNavigatorKey,
this.redirect,
List<RouteBase> routes = const <RouteBase>[],
}) : assert(path.isNotEmpty, 'GoRoute path cannot be empty'),
assert(name == null || name.isNotEmpty, 'GoRoute name cannot be empty'),
assert(pageBuilder != null || builder != null || redirect != null,
'builder, pageBuilder, or redirect must be provided'),
super._(
routes: routes,
) {
// cache the path regexp and parameters
_pathRE = patternToRegExp(path, _pathParams);
assert(() {
// check path params
final Map<String, List<String>> groupedParams =
_pathParams.groupListsBy<String>((String p) => p);
final Map<String, List<String>> dupParams =
Map<String, List<String>>.fromEntries(
groupedParams.entries
.where((MapEntry<String, List<String>> e) => e.value.length > 1),
);
assert(dupParams.isEmpty,
'duplicate path params: ${dupParams.keys.join(', ')}');
// check sub-routes
for (final RouteBase route in routes) {
// check paths
if (route is GoRoute) {
assert(
route.path == '/' ||
(!route.path.startsWith('/') && !route.path.endsWith('/')),
'sub-route path may not start or end with /: ${route.path}');
}
}
return true;
}());
}