route 0.4.6

  • Installing
  • Versions
  • 68


Route is a client + server routing library for Dart that helps make building single-page web apps and using HttpServer easier.


Add this package to your pubspec.yaml file:

  route: any

Then, run pub install to download and link in the package.


Route is built around UrlPattern a class that matches, parses and produces URLs. A UrlPattern is similar to a regex, but constrained so that it is reversible, given a UrlPattern and a set of arguments you can produce the URL that when parsed returns those same arguments. This is important for keeping the URL space for your app flexible so that you can change a URL for a resource in one place and keep your app working.

Route lets you use the same URL patterns for client-side and server-side routing. Just define a library containing all your URLs.

As an example, consider a blog with a home page and an article page. The article URL has the form /article/1234. We want to show articles without reloading the page.

Example (urls.dart):

library urls;

import 'package:route/url_pattern.dart';

final homeUrl = new UrlPattern(r'/');
final articleUrl = new UrlPattern(r'/article/(\d+)');
final allUrls = [homeUrl, articleUrl];

Client Routing

On the client, there is a Router class that associates UrlPatterns to handlers. Given a URL, the router finds a pattern that matches, and invokes it's handler. This is similar to HttpServer.addRequestHandler(matcher, handler) on the server. The handlers are then responsible for rendering the appropriate changes to the page.

The Router can listen to Window.onPopState events and invoke the correct handler so that the back button seamlessly works.

Example (client.dart):

library client;

import 'package:route/client.dart';

main() {
  var router = new Router()
    ..addHandler(homeUrl, showHome)
    ..addHandler(articleUrl, showArticle)

void showHome(String path) {
  // nothing to parse from path, since there are no groups

void showArticle(String path) {
  var articleId = articleUrl.parse(req.path)[0];
  // show article page with loading indicator
  // load article from server, then render article

Server Routing

On the server, route gives you a utility function to match HttpRequests against UrlPatterns.

import 'urls.dart';
import 'package:route/server.dart';
import 'package:route/pattern.dart';

HttpServer.bind().then((server) {
  var router = new Router(server)
    ..filter(matchesAny(allUrls), authFilter)
    ..serve(articleUrl, method: 'GET').listen(serveArticle)

Future<bool> authFilter(req) {
  return getUser(getUserIdCookie(req)).then((user) {
    if (user != null) {
      return true;
    return false;

serveArcticle(req) {
  var articleId = articleUrl.parse(req.uri.path)[0];
  // retrieve article data and respond

Further Goals

  • Integration with Polymer so that the changing of UI views can happen automatically.
  • Handling different HTTP methods to help implement REST APIs.
  • Automatic generation of REST URLs from a single URL pattern, similar to Ruby on Rails
  • Helpers for nested views and key-value URL schemes common with complex apps.
  • [Done] ~~Server-side routing for the dart:io v2 HttpServer~~
  • [Done] ~~IE 9 support~~

1. Depend on it

Add this to your package's pubspec.yaml file:

  route: "^0.4.6"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:route/client.dart';

import 'package:route/pattern.dart';

import 'package:route/server.dart';

import 'package:route/url_pattern.dart';
Version Uploaded Documentation Archive
0.4.6 Nov 12, 2013 Go to the documentation of route 0.4.6 Download route 0.4.6 archive
0.4.5+1 Jun 27, 2013 Go to the documentation of route 0.4.5+1 Download route 0.4.5+1 archive
0.4.5 Apr 22, 2013 Go to the documentation of route 0.4.5 Download route 0.4.5 archive
0.4.4 Apr 19, 2013 Go to the documentation of route 0.4.4 Download route 0.4.4 archive
0.4.3 Apr 17, 2013 Go to the documentation of route 0.4.3 Download route 0.4.3 archive
0.4.2 Mar 19, 2013 Go to the documentation of route 0.4.2 Download route 0.4.2 archive
0.0.4 Mar 8, 2013 Go to the documentation of route 0.0.4 Download route 0.0.4 archive
0.0.3 Feb 28, 2013 Go to the documentation of route 0.0.3 Download route 0.0.3 archive
0.0.2 Feb 22, 2013 Go to the documentation of route 0.0.2 Download route 0.0.2 archive
0.0.1 Feb 8, 2013 Go to the documentation of route 0.0.1 Download route 0.0.1 archive


This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Jan 17, 2018
  • Dart: 2.0.0-dev.15.0
  • pana: 0.9.1


Describes how popular the package is relative to other packages. [more]
83 / 100
Code health derived from static analysis. [more]
88 / 100
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]


Detected platforms: Flutter, server, web

Multiple platform identified in libraries.


  • Fix lib/url_pattern.dart.

    Strong-mode analysis of lib/url_pattern.dart failed with the following error:

    line: 196 col: 3
    Invalid override. The type of 'UrlPattern.allMatches' ('(String) → Iterable<Match>') isn't a subtype of 'Pattern.allMatches' ('(String, [int]) → Iterable<Match>').

  • Fix lib/pattern.dart.

    Strong-mode analysis of lib/pattern.dart failed with the following error:

    line: 23 col: 3
    Invalid override. The type of '_MultiPattern.allMatches' ('(String) → Iterable<Match>') isn't a subtype of 'Pattern.allMatches' ('(String, [int]) → Iterable<Match>').

  • Maintain

    Changelog entries help clients to follow the progress in your code.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 2 error(s) and 0 warning(s).

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use route.dart.


Package Constraint Resolved Available
Direct dependencies
browser >=0.9.0 <2.0.0 0.10.0+2
logging >=0.9.0 <2.0.0 0.11.3+1
Dev dependencies
path >=0.9.0 <2.0.0
unittest >=0.9.0 <2.0.0