Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Forked from anonymous/main.dart
Last active April 27, 2017 22:06
Show Gist options
  • Save matanlurey/ef4d939121c5ed3d2e942272e1bbdf98 to your computer and use it in GitHub Desktop.
Save matanlurey/ef4d939121c5ed3d2e942272e1bbdf98 to your computer and use it in GitHub Desktop.
import 'dart:async';
main() {
final router = new Router();
router.listen(print);
router.route(const Route('/', query: const {'referredId': '1234'}));
router.route(const Route('/contact'));
}
/// A simple example of a 'Router' that is just a stream of [Route].
class Router extends Stream<Route> {
final StreamController<Route> _onRoute = new StreamController<Route>();
@override
StreamSubscription<Route> listen(
void onData(Route route), {
Function onError,
void onDone(),
bool cancelOnError,
}) {
return _onRoute.stream.listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
}
/// Route to a new [route].
void route(Route route) {
_onRoute.add(route);
}
}
/// A simple route representation.
class Route {
final String path;
final Map<String, String> query;
const Route(this.path, {this.query: const {}});
@override
String toString() => '$Route {$path $query}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment