GoRoute constructor

GoRoute({
  1. required String path,
  2. String? name,
  3. GoRouterWidgetBuilder? builder,
  4. GoRouterPageBuilder? pageBuilder,
  5. GlobalKey<NavigatorState>? parentNavigatorKey,
  6. GoRouterRedirect? redirect,
  7. List<RouteBase> routes = const <RouteBase>[],
})

Constructs a GoRoute.

  • path and name cannot be empty strings.
  • One of either builder or pageBuilder 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;
  }());
}