parse_route 2.1.0 parse_route: ^2.1.0 copied to clipboard
All-encompassing solution for route parsing, matching, and navigation within Dart applications
Basic Route Registration and Matching #
import 'package:parse_route/parse_route.dart';
void main() {
final parser = ParseRoute();
// Register routes
parser.registerRouter('/user/:id');
parser.registerRouter('/profile/followers');
// Match routes
final result1 = parser.matchRoute('/user/123');
print(result1?.parameters); // {id: 123}
final result2 = parser.matchRoute('/profile/followers');
print(result2?.path); // /profile/followers
}
copied to clipboard
Navigation #
final parser = ParseRoute();
parser.registerRouter('/home');
parser.registerRouter('/profile/:id');
// Navigate to a route
parser.push('/home');
// Navigate with parameters
parser.push('/profile/123');
// Go back
parser.pop();
// Replace current route
parser.replaceLast('/settings');
copied to clipboard
Route Listeners #
parser.addListener('/profile', (newRoute, oldRoute, type) {
print('Profile route changed: ${newRoute.fullPath}');
});
parser.push('/profile/123'); // Triggers listener
copied to clipboard
Complex Routes #
parser.registerRouter('/project/:projectId/task/:taskId/detail');
final result = parser.matchRoute('/project/42/task/108/detail?foo=bar');
print(result?.parameters); // {projectId: 42, taskId: 108}
print(result?.urlParameters); // {foo: bar}
copied to clipboard
API Reference #
ParseRoute #
registerRouter(String path)
: Register a new routematchRoute(String path)
: Match a given path to a registered routepush(String path)
: Navigate to a new routepop()
: Go back to the previous routereplaceLast(String path)
: Replace the current routeaddListener(String path, RouteListener listener)
: Add a listener for route changesremoveListener(String path)
: Remove a route listener
MatchResult #
path
: The matched route pathcleanPath
: The actual path without query parametersparameters
: Map of route parametersurlParameters
: Map of URL query parameters