Dart Documentationclean_router.commonRouter

Router class

Router consists of multiple named Routes and provides methods for translating Routes to url/path and vice versa.

class Router {

 final Map<String, Route> _routes;
 String _host;

 Router(this._host, this._routes);

 /**
  * Registeres a [route] identified by [routeName] in [Router].
  * It is not allowed to override already registered routes.
  */
 void addRoute(String routeName, Route route) {
   if(_routes.containsKey(routeName)) {
     throw new ArgumentError("Route name '$routeName' already in use in Route.");
   }
   if(routeName == PARAM_ROUTE_NAME) {
     throw new ArgumentError("Route name cannot be '$PARAM_ROUTE_NAME' in PageNavigator.");
   }
   _routes[routeName] = route;
 }

 /**
  * Returns path part of the url corresponding to the given [routeName] and
  * [parameters]. Accepts [Map] and [Data] as [parameters].
  */
 String routePath(String routeName, dynamic parameters) {
   var route = this._routes[routeName];
   if (route == null) {
     throw new ArgumentError('Router does not contain a route "$routeName".');
   }
   return this._routes[routeName].path(parameters);
 }

 /**
  * Returns the whole url corresponding to the given [routeName] and
  * [parameters]. Accepts both [Map] and [Data] as [parameters].
  */
 String routeUrl(String routeName, dynamic parameters) {
   String path = this.routePath(routeName, parameters);
   return this._routes[routeName].isAbsolute ? path : this._host + path;
 }

 /**
  * Returns the List [[routeName, matchedParameters]] matching the [url].
  */
 List match(String url) {
   for (var key in this._routes.keys) {
     var match = this._routes[key].match(url);
     if (match != null) {
       match[PARAM_ROUTE_NAME] = key;
       return [key, match];
     }
   }
   return null;
 }
}

Constructors

new Router(String _host, Map<String, Route> _routes) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
Router(this._host, this._routes);

Methods

void addRoute(String routeName, Route route) #

Registeres a route identified by routeName in Router. It is not allowed to override already registered routes.

void addRoute(String routeName, Route route) {
 if(_routes.containsKey(routeName)) {
   throw new ArgumentError("Route name '$routeName' already in use in Route.");
 }
 if(routeName == PARAM_ROUTE_NAME) {
   throw new ArgumentError("Route name cannot be '$PARAM_ROUTE_NAME' in PageNavigator.");
 }
 _routes[routeName] = route;
}

List match(String url) #

Returns the List [[routeName, matchedParameters]] matching the url.

List match(String url) {
 for (var key in this._routes.keys) {
   var match = this._routes[key].match(url);
   if (match != null) {
     match[PARAM_ROUTE_NAME] = key;
     return [key, match];
   }
 }
 return null;
}

String routePath(String routeName, parameters) #

Returns path part of the url corresponding to the given routeName and parameters. Accepts Map and Data as parameters.

String routePath(String routeName, dynamic parameters) {
 var route = this._routes[routeName];
 if (route == null) {
   throw new ArgumentError('Router does not contain a route "$routeName".');
 }
 return this._routes[routeName].path(parameters);
}

String routeUrl(String routeName, parameters) #

Returns the whole url corresponding to the given routeName and parameters. Accepts both Map and Data as parameters.

String routeUrl(String routeName, dynamic parameters) {
 String path = this.routePath(routeName, parameters);
 return this._routes[routeName].isAbsolute ? path : this._host + path;
}