Skip to content

Commit

Permalink
nnbd second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepighetti committed Jan 31, 2021
1 parent c88a532 commit 4f53b1a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 48 deletions.
1 change: 1 addition & 0 deletions example/lib/config/route_handlers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var demoFunctionHandler = Handler(
);
},
);
return;
});

/// Handles deep links into the app
Expand Down
1 change: 1 addition & 0 deletions example/lib/config/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Routes {
router.notFoundHandler = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
print("ROUTE WAS NOT FOUND !!!");
return;
});
router.define(root, handler: rootHandler);
router.define(demoSimple, handler: demoRouteHandler);
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ packages:
version: "2.1.0-nullsafety.5"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.17.0"
flutter: ">=1.17.0 <2.0.0"
22 changes: 11 additions & 11 deletions lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ enum HandlerType {

/// The handler to register with [FluroRouter.define]
class Handler {
Handler({this.type = HandlerType.route, this.handlerFunc});
final HandlerType? type;
final HandlerFunc? handlerFunc;
Handler({this.type = HandlerType.route, required this.handlerFunc});
final HandlerType type;
final HandlerFunc handlerFunc;
}

/// A function that creates new routes.
typedef Route<T> RouteCreator<T>(
RouteSettings? route, Map<String, List<String>>? parameters);
RouteSettings route, Map<String, List<String>>? parameters);

/// Builds out a screen based on string path [parameters] and context.
///
/// Note: you can access [RouteSettings] with the [context.settings] extension
typedef Widget HandlerFunc(
typedef Widget? HandlerFunc(
BuildContext? context, Map<String, List<String>>? parameters);

/// A route that is added to the router tree.
class AppRoute {
String? route;
dynamic? handler;
String route;
dynamic handler;
TransitionType? transitionType;
Duration? transitionDuration;
RouteTransitionsBuilder? transitionBuilder;
Expand Down Expand Up @@ -77,14 +77,14 @@ class RouteMatch {
this.route,
this.errorMessage = "Unable to match route. Please check the logs."});
final Route<dynamic>? route;
final RouteMatchType? matchType;
final String? errorMessage;
final RouteMatchType matchType;
final String errorMessage;
}

/// When the route is not found.
class RouteNotFoundException implements Exception {
final String? message;
final String? path;
final String message;
final String path;
RouteNotFoundException(this.message, this.path);

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'package:flutter/material.dart';
extension FluroBuildContextX on BuildContext {
/// Convenience method to retreive [RouteSettings] via
/// `ModalRoute.of(context).settings`
RouteSettings? get settings => ModalRoute.of(this)!.settings;
RouteSettings? get settings => ModalRoute.of(this)?.settings;

/// Helper to get [RouteSettings.arguments] via
/// `ModalRoute.of(context).settings.arguments`
Object? get arguments => ModalRoute.of(this)!.settings.arguments;
Object? get arguments => ModalRoute.of(this)?.settings.arguments;
}
21 changes: 12 additions & 9 deletions lib/src/fluro_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'dart:async';

import 'package:fluro/fluro.dart';
import 'package:fluro/src/common.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -112,7 +111,8 @@ class FluroRouter {
settings: routeSettings,
maintainState: maintainState ?? true,
builder: (BuildContext context) {
return notFoundHandler!.handlerFunc!(context, parameters);
return notFoundHandler!.handlerFunc(context, parameters) ??
SizedBox.shrink();
});
};
return creator(RouteSettings(name: path), null);
Expand Down Expand Up @@ -142,8 +142,7 @@ class FluroRouter {
Handler handler = (route != null ? route.handler : notFoundHandler);
TransitionType? transition = transitionType;
if (transitionType == null) {
transition =
route != null ? route.transitionType : TransitionType.native;
transition = route != null ? route.transitionType : TransitionType.native;
}
if (route == null && notFoundHandler == null) {
return RouteMatch(
Expand All @@ -153,7 +152,7 @@ class FluroRouter {
Map<String, List<String>> parameters =
match?.parameters ?? <String, List<String>>{};
if (handler.type == HandlerType.function) {
handler.handlerFunc!(buildContext, parameters);
handler.handlerFunc(buildContext, parameters);
return RouteMatch(matchType: RouteMatchType.nonVisual);
}

Expand All @@ -167,7 +166,8 @@ class FluroRouter {
fullscreenDialog: transition == TransitionType.nativeModal,
maintainState: maintainState,
builder: (BuildContext context) {
return handler.handlerFunc!(context, parameters);
return handler.handlerFunc(context, parameters) ??
SizedBox.shrink();
});
} else if (transition == TransitionType.material ||
transition == TransitionType.materialFullScreenDialog) {
Expand All @@ -177,7 +177,8 @@ class FluroRouter {
transition == TransitionType.materialFullScreenDialog,
maintainState: maintainState,
builder: (BuildContext context) {
return handler.handlerFunc!(context, parameters);
return handler.handlerFunc(context, parameters) ??
SizedBox.shrink();
});
} else if (transition == TransitionType.cupertino ||
transition == TransitionType.cupertinoFullScreenDialog) {
Expand All @@ -187,7 +188,8 @@ class FluroRouter {
transition == TransitionType.cupertinoFullScreenDialog,
maintainState: maintainState,
builder: (BuildContext context) {
return handler.handlerFunc!(context, parameters);
return handler.handlerFunc(context, parameters) ??
SizedBox.shrink();
});
} else {
RouteTransitionsBuilder? routeTransitionsBuilder;
Expand All @@ -204,7 +206,8 @@ class FluroRouter {
maintainState: maintainState,
pageBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return handler.handlerFunc!(context, parameters);
return handler.handlerFunc(context, parameters) ??
SizedBox.shrink();
},
transitionDuration: transition == TransitionType.none
? Duration.zero
Expand Down
22 changes: 9 additions & 13 deletions lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class AppRouteMatch {
AppRouteMatch(this.route);

// properties
AppRoute? route;
Map<String, List<String>>? parameters = <String, List<String>>{};
AppRoute route;
Map<String, List<String>> parameters = <String, List<String>>{};
}

/// A matched [RouteTreeNode]
Expand All @@ -39,7 +39,7 @@ class RouteTreeNodeMatch {
}

// properties
RouteTreeNode? node;
RouteTreeNode node;
Map<String, List<String>> parameters = <String, List<String>>{};
}

Expand All @@ -49,7 +49,7 @@ class RouteTreeNode {
RouteTreeNode(this.part, this.type);

// properties
String? part;
String part;
RouteTreeNodeType? type;
List<AppRoute>? routes = <AppRoute>[];
List<RouteTreeNode>? nodes = <RouteTreeNode>[];
Expand All @@ -68,7 +68,7 @@ class RouteTree {

// addRoute - add a route to the route tree
void addRoute(AppRoute route) {
String path = route.route!;
String path = route.route;
// is root/default route, just add it
if (path == Navigator.defaultRouteName) {
if (_hasDefaultRoute) {
Expand Down Expand Up @@ -142,13 +142,12 @@ class RouteTree {
RouteTreeNodeMatch match =
RouteTreeNodeMatch.fromMatch(parentMatch, node);
if (node.isParameter()) {
String paramKey = node.part!.substring(1);
String paramKey = node.part.substring(1);
match.parameters[paramKey] = [pathPart];
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
}
// print("matched: ${node.part}, isParam: ${node.isParameter()}, params: ${match.parameters}");
currentMatches[node] = match;
if (node.nodes != null) {
nextNodes.addAll(node.nodes!);
Expand All @@ -165,11 +164,8 @@ class RouteTree {
if (matches.length > 0) {
RouteTreeNodeMatch match = matches.first;
RouteTreeNode? nodeToUse = match.node;
// print("using match: ${match}, ${nodeToUse?.part}, ${match?.parameters}");
if (nodeToUse != null &&
nodeToUse.routes != null &&
nodeToUse.routes!.length > 0) {
List<AppRoute> routes = nodeToUse.routes!;
final routes = nodeToUse.routes;
if (routes != null && routes.length > 0) {
AppRouteMatch routeMatch = AppRouteMatch(routes[0]);
routeMatch.parameters = match.parameters;
return routeMatch;
Expand All @@ -196,7 +192,7 @@ class RouteTree {
}
}

RouteTreeNode? _nodeForComponent(String? component, RouteTreeNode? parent) {
RouteTreeNode? _nodeForComponent(String component, RouteTreeNode? parent) {
List<RouteTreeNode> nodes = _nodes;
if (parent != null) {
// search parent for sub-node matches
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ packages:
version: "2.1.0-nullsafety.5"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.17.0"
flutter: ">=1.17.0 <2.0.0"
17 changes: 6 additions & 11 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:fluro/fluro.dart';

void main() {
testWidgets("FluroRouter correctly parses named parameters",
(WidgetTester tester) async {
test("FluroRouter correctly parses named parameters", () async {
String path = "/users/1234";
String route = "/users/:id";
FluroRouter router = FluroRouter();
Expand All @@ -25,8 +24,7 @@ void main() {
}));
});

testWidgets("FluroRouter correctly parses named parameters with query",
(WidgetTester tester) async {
test("FluroRouter correctly parses named parameters with query", () async {
String path = "/users/1234?name=luke";
String route = "/users/:id";
FluroRouter router = FluroRouter();
Expand All @@ -40,8 +38,7 @@ void main() {
}));
});

testWidgets("FluroRouter correctly parses query parameters",
(WidgetTester tester) async {
test("FluroRouter correctly parses query parameters", () async {
String path = "/users/create?name=luke&phrase=hello%20world&number=7";
String route = "/users/create";
FluroRouter router = FluroRouter();
Expand All @@ -56,8 +53,7 @@ void main() {
}));
});

testWidgets("FluroRouter correctly parses array parameters",
(WidgetTester tester) async {
test("FluroRouter correctly parses array parameters", () async {
String path =
"/users/create?name=luke&phrase=hello%20world&number=7&number=10&number=13";
String route = "/users/create";
Expand All @@ -72,14 +68,13 @@ void main() {
"number": ["7", "10", "13"],
}));
});
testWidgets("FluroRouter correctly matches route and transition type",
(WidgetTester tester) async {
test("FluroRouter correctly matches route and transition type", () async {
String path = "/users/1234";
String route = "/users/:id";
FluroRouter router = FluroRouter();
router.define(route,
handler: null, transitionType: TransitionType.inFromRight);
AppRouteMatch? match = router.match(path);
expect(TransitionType.inFromRight, match?.route?.transitionType);
expect(TransitionType.inFromRight, match?.route.transitionType);
});
}

0 comments on commit 4f53b1a

Please sign in to comment.