MagicRouter class

The Magic Router.

This is the central routing service that wraps GoRouter with a Laravel-style API. It manages all route definitions and provides context-free navigation methods.

Registration

Routes are registered using the MagicRoute facade:

MagicRoute.page('/', () => HomePage());
MagicRoute.page('/users/:id', (id) => UserPage(id: id));

Navigate from anywhere - controllers, services, or pure Dart classes:

MagicRoute.to('/dashboard');
MagicRoute.back();
MagicRoute.replace('/home');

Setup

Use routerConfig with MaterialApp.router:

MaterialApp.router(
  routerConfig: MagicRouter.instance.routerConfig,
)

Properties

allRoutes → List<RouteDefinition>
Every registered route, including those declared inside a layout group.
no setter
browserBack ↔ void Function()?
Steps the browser history back one entry; null off the web.
getter/setter pair
currentLocation → String?
Get the current route location (path + query string).
no setter
currentPath → String?
Get the current route path (without query string).
no setter
currentRoute → RouteDefinition?
Get the RouteDefinition resolved for the current location.
no setter
defaultStacked ↔ bool
Whether to pushes rather than replaces, for a route that does not say.
getter/setter pair
defaultTransition ↔ RouteTransition
The transition a route takes when it does not name one.
getter/setter pair
hashCode → int
The hash code for this object.
no setterinherited
hasIntendedUrl → bool
Whether there is a pending intended URL.
no setter
historyDepth → int
The number of entries in the navigation history.
no setter
mergedLayouts → List<LayoutDefinition>
Merged layout definitions. Exposed for testing layout merging behavior.
no setter
The global navigator key for context-free navigation.
final
observers → List<NavigatorObserver>
Get all registered navigator observers.
no setter
overlayPresentation ↔ MagicOverlayPresentation Function(BuildContext context)?
Picks the frame each overlay route opens in; null keeps every overlay a bottom sheet.
getter/setter pair
pathParameters → Map<String, String>
Get all path parameters.
no setter
queryParameters → Map<String, String>
Get all query parameters.
no setter
routerConfig → GoRouter
Get the GoRouter configuration for MaterialApp.router.
no setter
routes → List<RouteDefinition>
Get all registered routes.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

addLayout(LayoutDefinition layout) → void
Add a layout (shell) definition.
addObserver(NavigatorObserver observer) → void
Add a navigator observer.
addRoute(RouteDefinition route) → void
Add a route definition.
back({String? fallback}) → void
Go back to the previous route.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pathParameter(String key) → String?
Get a path parameter from the current route.
pathParameterOf(BuildContext context, String key) → String?
Get a path parameter from the route page that context sits in.
pullIntendedUrl() → String?
Get and clear the intended URL (one-time read).
push(String path) → void
Push a new route onto the stack.
queryParameter(String key) → String?
Get a query parameter from the current route.
queryParameterOf(BuildContext context, String key) → String?
Get a query parameter from the route page that context sits in.
replace(String path) → void
Replace the current route.
setInitialLocation(String location) → void
Set the initial route location.
setIntendedUrl(String url) → void
Save an intended URL before redirecting to login.
startCollection() → void
Start collecting routes for a layout group.
stopCollection() → List<RouteDefinition>
Stop collecting routes and return them.
to(String path, {Map<String, String>? queryParameters}) → void
Navigate to a path.
toNamed(String name, {Map<String, String> pathParameters = const {}, Map<String, String> queryParameters = const {}}) → void
Navigate to a named route.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

instance → MagicRouter
Access the global router instance.
no setter

Static Methods

reset() → void
Reset the router (useful for testing).