m4d_router 0.2.14

  • Readme
  • Changelog
  • Installing
  • 62

Material4Dart router #

m4d_router is a client routing library for Dart. It helps make building single-page web apps and using HttpServer easier.

Preview

Installation #

Add this package to your pubspec.yaml file:

dependencies:
  m4d_router: any

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

Example #

Live: m4d_router.example.mikemitterer.at
Source on GitHub

void main() {
    final router = new Router();

    configLogging(show: Level.INFO);
    _configRouter(router);

    dom.querySelector('#output').text = 'Your Dart app is running.';
}

void _configRouter(final Router router ) {

    router
        ..addRoute(name: "Test I", path: new UrlPattern('/#/test'),
            enter: (final RouteEnterEvent event) {
                _log(event.route.title);
                _logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
                _showImage("https://upload.wikimedia.org/wikipedia/commons/1/11/Test-Logo.svg");
            })

        ..addRoute(name: 'Cats', path: new ReactPattern('/cats'),
            enter: (final RouteEnterEvent event) {
                _log(event.route.title);
                _logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
                _showImage("https://i1.wp.com/www.oxygen.ie/wp-content/uploads/2016/11/main_1500.jpg?resize=750%2C400");
            })

        ..addRoute(name: 'Specific cat', path: new ReactPattern(r'/cats/(\w+)'),
            enter: (final RouteEnterEvent event) {
                _log("${event.route.title}: ${event.params.join(",")}");
                _logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
                if(event.params.first.toLowerCase() == "grumpy") {
                    _showImage("https://pbs.twimg.com/media/CsW0pmxUsAAuvEN.jpg");
                } else {
                    _showImage("https://catzone-tcwebsites.netdna-ssl.com/wp-content/uploads/2014/09/453768-cats-cute.jpg");
                }
            })

        ..addRoute(name: "Google for cats", path: new ReactPattern('/google'),
            enter: (final RouteEnterEvent event) {
                _log(event.route.title);
                _logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
                _showImage("https://upload.wikimedia.org/wikipedia/commons/a/a5/Google_Chrome_icon_%28September_2014%29.svg");
            })
    ;

    // optional
    router.onEnter.listen((final RouteEnterEvent event) {
        _logger.info("RoutEvent ${event.route.title} -> ${event.route.urlPattern.pattern}");
    });

    // optional
    router.onError.listen((final RouteErrorEvent event) {
        _logger.info("RouteErrorEvent ${event.exception}");
    });

    router.listen(); // Start listening
}

void _log(final String logMessage) {
    final logElement = dom.querySelector("#log") as dom.UListElement;
    logElement.append(new dom.LIElement()..text = logMessage);
}

void _showImage(final String url) {
    final img = dom.querySelector("img") as dom.ImageElement;
    img.src = url;
}

UrlPattern #

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.

Routing #

On the browser, there is a Router class that associates UrlPatterns to handlers. Given a URL, the router finds a pattern that matches, and invokes its handler. The handlers are then responsible for rendering the appropriate changes to the page.

Change Log for m4d_router #

A browser routing library for Dart

Unreleased #

Bugs #

  • build.jenkins uses the right deploy-samples-script 1f877bc

Docs #

  • Link to autogenerated example b105179

v0.2 - 2018-06-27

Bugs #

  • deploy-sample.sh failed on Linux 3a9c29e
  • deploy-sample.sh failed on Linux 456927c
  • Jenkins could not find 'deploy-sample'-script - (plural) 63c4187
  • Jenkins could not find 'deploy-samples'-script - extension missing 2dc1317
  • In Dart 2.x List converts to JSArray, changed List to generic List

Docs #

  • Link to live sample in README 9b3cb91
  • Nicer sample, preparation for Jenkins fb9fe14
  • Changed images in animated gif 59f8fbb
  • Preview-Image added 2785148
  • Preview-Image added 444f6b8

Test #

This CHANGELOG.md was generated with Changelog for Dart

Use this package as a library

1. Depend on it

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


dependencies:
  m4d_router: ^0.2.14

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:m4d_router/browser.dart';
import 'package:m4d_router/exceptions.dart';
import 'package:m4d_router/exeptions/RouteNotFoundException.dart';
import 'package:m4d_router/route.dart';
import 'package:m4d_router/router.dart';
import 'package:m4d_router/url_pattern.dart';
import 'package:m4d_router/utils/async_utils.dart';
import 'package:m4d_router/utils/map_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
81
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
47
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Jan 17, 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 suggestions

Fix lib/browser.dart. (-10.89 points)

Analysis of lib/browser.dart reported 23 hints, including:

line 25 col 17: Unnecessary new keyword.

line 38 col 21: Unnecessary new keyword.

line 51 col 52: Unnecessary new keyword.

line 58 col 35: Use = to separate a named parameter from its default value.

line 59 col 23: Unnecessary new keyword.

Fix lib/url_pattern.dart. (-8.17 points)

Analysis of lib/url_pattern.dart reported 17 hints, including:

line 10 col 23: Unnecessary new keyword.

line 12 col 48: Unnecessary new keyword.

line 80 col 64: Use = to separate a named parameter from its default value.

line 81 col 24: Unnecessary new keyword.

line 107 col 47: Unnecessary new keyword.

Fix lib/utils/async_utils.dart. (-1 points)

Analysis of lib/utils/async_utils.dart reported 2 hints:

line 17 col 15: Unnecessary new keyword.

line 18 col 9: Unnecessary new keyword.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/exceptions.dart (Run dartfmt to format lib/exceptions.dart.)
  • lib/exeptions/RouteNotFoundException.dart (Run dartfmt to format lib/exeptions/RouteNotFoundException.dart.)
  • lib/route.dart (Run dartfmt to format lib/route.dart.)
  • lib/router.dart (Run dartfmt to format lib/router.dart.)
  • lib/utils/map_utils.dart (Run dartfmt to format lib/utils/map_utils.dart.)

Maintenance suggestions

Package is getting outdated. (-32.60 points)

The package was last published 69 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and m4d_router.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.0.0 <3.0.0
logging >=0.11.3 <1.0.0 0.11.4
uri ^0.11.0 0.11.3+1
validate ^1.0.0 1.7.0
Transitive dependencies
matcher 0.12.6
meta 1.1.8
quiver 2.1.2+1
stack_trace 1.9.3
utf 0.9.0+5
Dev dependencies
build_runner any
build_test any
build_web_compilers any
console_log_handler ^1.0.0
mockito any
path ^1.6.0 1.6.4
test any