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
ParseRoute Library #
Designed with efficiency and comprehensiveness in mind, ParseRoute delivers an all-encompassing solution for route parsing, matching, and navigation within Dart applications. Its intuitive API caters to developers by simplifying complex navigation management tasks, making it a highly flexible and scalable choice for integration into various applications and frameworks. Ideal for projects utilizing Navigator 2, or even in server-side Dart applications, ParseRoute is poised to become a foundational component of GetX and GetServer at some point in the future.
Key Features #
- Dynamic Route Matching: Simplifies the matching of routes, including those with dynamic segments, and effortlessly extracts relevant parameters for use.
- Query Parameter Parsing: Integrates the parsing of URL query parameters directly into the route matching workflow, enhancing data retrieval and usage.
- Route Registration & Verification: Enables the easy registration of routes within your application and allows for the immediate verification of their registration status.
- Nested Route Support: Facilitates the organization of complex application navigational structures through the use of nested routes.
- Advanced Programmatic Navigation: Provides robust tools for programmatically managing navigation, leveraging both route and query parameters for seamless transitions.
- Comprehensive History Management: Features a powerful history management system that supports back navigation, route replacement, and history clearing functionalities.
Getting Started #
To integrate ParseRoute into your Dart project, simply add it as a dependency in your project's pubspec.yaml
file:
dependencies:
parse_route: ^<latest_version>
Ensure to replace <latest_version>
with the latest version number of the library.
Basic Usage #
Setting Up #
Begin by importing ParseRoute into your project file:
import 'package:parse_route/parse_route.dart';
Register Routes #
Register your application's routes as follows:
final parser = ParseRoute();
// Register individual routes with dynamic segments
parser.registerRoute('/user/:id');
// Automatic tracking of nested routes
parser.registerRoute('/profile/followers');
parser.registerRoute('/profile/following');
parser.registerRoute('/profile/edit');
// Wildcard support
parser.registerRoute('/settings/*');
// Query parameter support
parser.registerRoute('/search?foo=bar&baz=qux');
// Dynamic segments can be registered with query parameters
parser.registerRoute('/user/:id?foo=bar');
Route Matching and Navigation #
Match a route and navigate through your application with ease:
final match = parser.matchRoute('/user/123?foo=bar');
if (match != null) {
// Access matched route and parameters
print(match.path); // /user/:id
print(match.parameters); // {id: '123'}
print(match.urlParameters); // {foo: 'bar'}
}
// Navigate to a route
parser.push('/profile/123');
// Check the current route
print(parser.current.fullPath); // /profile/123
Managing Navigation History #
Efficiently manage your navigation history:
// Navigate through routes
parser.push('/home');
parser.push('/profile/edit');
// Go back to the previous route
parser.pop();
// Verify the current route
print(parser.current.fullPath); // /home
Route Listeners #
parser.addListener('/profile', (newRoute, oldRoute, type) {
print('Profile route changed: ${newRoute.fullPath}');
});
parser.push('/profile/123'); // Triggers listener
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}
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
Contribution and Support #
We welcome contributions to the ParseRoute library with open arms! If you're interested in contributing, please submit a pull request with your proposed changes. For any questions, suggestions, or issues, feel free to open an issue on our GitHub repository. Your input helps make ParseRoute better.