Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/go_router/example/lib/path_parameter_regex.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

/// Router configuration demonstrating regex-constrained path parameters.
final GoRouter router = GoRouter(
routes: <GoRoute>[
GoRoute(
path: r'/users/:id(\d+)',
builder: (BuildContext context, GoRouterState state) {
return Scaffold(body: Center(child: Text('User ${state.pathParameters['id']}')));
},
),
],
);

/// Runs the path parameter regular expression example.
void main() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you create a simple test just to make sure everything compile

runApp(MaterialApp.router(routerConfig: router));
}
15 changes: 15 additions & 0 deletions packages/go_router/lib/src/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: doc_directive_unknown

import 'dart:async';

import 'package:collection/collection.dart';
Expand Down Expand Up @@ -341,6 +343,19 @@ class GoRoute extends RouteBase {
/// `/family/456` and etc. The parameter values are stored in [GoRouterState]
/// that are passed into [pageBuilder] and [builder].
///
/// A path parameter may optionally be constrained to a regular expression
/// by appending the expression in parentheses after the parameter name:
/// `:paramName(regex)`.
///
/// {@example /example/lib/path_parameter_regex.dart}
///
/// The path matches `/users/42` but not `/users/settings`. If a path segment
/// does not satisfy the regular expression, route matching continues with
/// other route candidates.
///
/// The regular expression is interpreted as a Dart [RegExp] pattern and must
/// not contain nested parentheses.
///
/// The query parameter are also capture during the route parsing and stored
/// in [GoRouterState].
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Documents support for regular expression constraints in GoRoute path parameters.
version: patch
Loading