route_pattern_generator 0.5.17

route_pattern_generator #

A Dart static code generator that produces matchers and builders from route URI patterns.

Quickstart #

Define your patterns as constant strings annoted with @route.

import 'package:route_pattern/route_pattern.dart';
import 'package:flutter/widgets.dart';

part 'routes.g.dart';

Route home(RouteSettings settings, HomeRouteArguments arguments) {
    // returns a Route

Route article(RouteSettings settings, ArticleRouteArguments arguments) {
    // returns a Route

Each constant will generate a Route with a build and match method, and a associated Argument class (an example of the generate sources is available in the sample).

// Build specific routes
final path = "users"));
expect(path, "/?tab=users");

final path = "12345"));
expect(path, "/article/12345");

// Match specific routes
final match = Routes.home.match("/?tab=users");
expect(match.isSuccess, true);
expect(, 'users');

final match = Routes.article.match("/article/12345");
expect(match.isSuccess, true);
expect(, '12345');

// Or get the first matching route
final match = Routes.match("/article/12345");
if(match is MatchResult<ArticleRouteArguments>) {
    expect(, '12345');

A Routes.onGenerateRoute method is also generated to use in your app or navigator. It will match the first route which settings name matches a pattern and call your declared function with corresponding arguments.

import 'package:sample/routes.dart';
import 'package:flutter/material.dart';

class ExampleApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: Routes.onGenerateRoute,
      // ...

To navigate to a given route, a Routes.push method is also available :

await Routes.push(context, ArticleRouteArguments(id: '12345'));

How to use #

Install #

There are a few separate packages you need to install:



Pattern format #

A route pattern is composed of static segments separated with /, required parameters as dynamic segments (starting with :), and optional parameters as query parameters (starting with ? and separated by &).

Typing parameters #

By default, arguments are of type String, but a custom type surrounded with [ and ] can be added at the beginning of a required or optional parameter. This type must have static T parse(String value) and String toString() methods to serialize and deserialize arguments from path.

Example #


  • article : static segment
  • id : required dynamic segment of type int
  • details : static segment
  • tab : optionnal query parameter of type String
  • scroll : optionnal query parameter of type int

This example will match those URIs :

  • /article/26436/details
  • /article/1234/details?tab=second
  • /article/98904/details?tab=first&scroll=8

Run the generator #

To run the generator, you must use build_runner cli:

flutter pub pub run build_runner watch

Use this package as a library

1. Depend on it

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

  route_pattern_generator: ^0.5.17

2. Install it

You can install packages from the command line:

with pub:

$ pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:route_pattern_generator/builder.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 21, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health issues and suggestions

Document public APIs. (-1 points)

2 out of 2 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/builders.dart. (-1.99 points)

Analysis of lib/src/builders.dart reported 4 hints:

line 1 col 8: Unused import: 'package:analyzer/dart/element/element.dart'.

line 126 col 12: Prefer to use whereType on iterable.

line 139 col 10: Prefer to use whereType on iterable.

line 219 col 10: Prefer to use whereType on iterable.

Fix lib/src/parsing.dart. (-0.50 points)

Analysis of lib/src/parsing.dart reported 1 hint:

line 10 col 23: Use isNotEmpty instead of length

Format lib/builder.dart.

Run dartfmt to format lib/builder.dart.

Maintenance issues and suggestions

Provide a file named (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (recase).

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and route_pattern_generator.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
analyzer >=0.38.5 <0.40.0 0.39.4
build ^1.1.4 1.2.2
build_config >=0.3.1 <0.5.0 0.4.1+1
code_builder ^3.1.3 3.2.1
dart_style ^1.2.2 1.3.3
path ^1.3.2 1.6.4
recase ^2.0.1 2.0.1 3.0.0
route_pattern ^0.5.16 0.5.16
source_gen ^0.9.4 0.9.4+7
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.5.2
async 2.4.0
built_collection 4.3.2
built_value 7.0.8
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
fixnum 0.10.11
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
json_annotation 3.0.1
logging 0.11.4
matcher 0.12.6
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
pedantic 1.9.0
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.1.2+1
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.7.2
test ^1.3.3