route_pattern_generator 0.5.16

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';

@RoutePattern("/?tab&[int]scroll")
Route home(RouteSettings settings, HomeRouteArguments arguments) {
    // returns a Route
}

@RoutePattern("/article/:[int]id")
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 = Routes.home.build(HomeRouteArguments(tab: "users"));
expect(path, "/?tab=users");

final path = Routes.article.build(ArticleRouteArguments(id: "12345"));
expect(path, "/article/12345");

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

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

// Or get the first matching route
final match = Routes.match("/article/12345");
if(match is MatchResult<ArticleRouteArguments>) {
    expect(match.arguments.id, '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 {
  @override
  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:

dependencies:
  route_pattern:

dev_dependencies:
  route_pattern_generator: 
  build_runner: 

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/:[int]id/details?tab&[int]scroll

  • 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:


dependencies:
  route_pattern_generator: ^0.5.16

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: other

Platform components identified in package: build, io, mirrors.

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. (-0.50 points)

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

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

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 CHANGELOG.md. (-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 (analyzer).

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/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
analyzer >=0.34.0 <0.37.0 0.36.4 0.38.0
build ^1.0.0 1.1.6
build_config >=0.3.1 <0.5.0 0.4.1+1
code_builder ^3.1.3 3.2.0
dart_style ^1.2.2 1.2.9
path ^1.3.2 1.6.4
recase ^2.0.1 2.0.1
route_pattern ^0.5.16 0.5.16
source_gen ^0.9.0 0.9.4+3
Transitive dependencies
args 1.5.2
async 2.3.0
built_collection 4.2.2
built_value 6.7.0
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.1+1
csslib 0.16.1
fixnum 0.10.9
front_end 0.1.19 0.1.22
glob 1.1.7
html 0.14.0+2
json_annotation 3.0.0
kernel 0.3.19 0.3.22
logging 0.11.3+2
matcher 0.12.5
meta 1.1.7
package_config 1.1.0
pedantic 1.8.0+1
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.0.5
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.1.16
Dev dependencies
build_runner ^1.0.0
test ^1.3.3