Skip to content

Commit

Permalink
Merge pull request #220 from lukepighetti/null-safety
Browse files Browse the repository at this point in the history
null-safe nits and improvements
  • Loading branch information
lukepighetti authored Mar 1, 2021
2 parents 306d2d7 + ebecb8f commit a86e8b4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Handler {

/// 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(
BuildContext? context, Map<String, List<String>>? parameters);
BuildContext? context, Map<String, List<String>> parameters);

/// A route that is added to the router tree.
class AppRoute {
Expand Down
31 changes: 18 additions & 13 deletions lib/src/fluro_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ class FluroRouter {
/// Generic handler for when a route has not been defined
Handler? notFoundHandler;

/// The default transition duration to use throughout Fluro
static const defaultTransitionDuration = const Duration(milliseconds: 250);

/// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a default transition type.
void define(String routePath,
{required Handler? handler,
TransitionType? transitionType,
Duration? transitionDuration = const Duration(milliseconds: 250),
Duration transitionDuration = defaultTransitionDuration,
RouteTransitionsBuilder? transitionBuilder}) {
_routeTree.addRoute(
AppRoute(routePath, handler,
Expand Down Expand Up @@ -94,7 +97,7 @@ class FluroRouter {
}
completer.complete();
} else {
String error = "No registered route was found to handle '$path'.";
final error = "No registered route was found to handle '$path'.";
print(error);
completer.completeError(RouteNotFoundException(error, path));
}
Expand All @@ -106,16 +109,16 @@ class FluroRouter {
Route<Null> _notFoundRoute(BuildContext context, String path,
{bool? maintainState}) {
RouteCreator<Null> creator =
(RouteSettings? routeSettings, Map<String, List<String>>? parameters) {
(RouteSettings? routeSettings, Map<String, List<String>> parameters) {
return MaterialPageRoute<Null>(
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);
return creator(RouteSettings(name: path), {});
}

/// Attempt to match a route to the provided [path].
Expand All @@ -125,11 +128,9 @@ class FluroRouter {
Duration? transitionDuration,
RouteTransitionsBuilder? transitionsBuilder,
bool maintainState = true}) {
RouteSettings? settingsToUse = routeSettings;
if (routeSettings == null) {
settingsToUse = RouteSettings(name: path);
}
if (settingsToUse!.name == null) {
RouteSettings settingsToUse = routeSettings ?? RouteSettings(name: path);

if (settingsToUse.name == null) {
settingsToUse = settingsToUse.copyWith(name: path);
}
AppRouteMatch? match = _routeTree.matchRoute(path!);
Expand Down Expand Up @@ -157,7 +158,7 @@ class FluroRouter {
}

RouteCreator creator =
(RouteSettings? routeSettings, Map<String, List<String>>? parameters) {
(RouteSettings? routeSettings, Map<String, List<String>> parameters) {
bool isNativeTransition = (transition == TransitionType.native ||
transition == TransitionType.nativeModal);
if (isNativeTransition) {
Expand Down Expand Up @@ -211,10 +212,14 @@ class FluroRouter {
},
transitionDuration: transition == TransitionType.none
? Duration.zero
: (transitionDuration ?? route?.transitionDuration)!,
: (transitionDuration ??
route?.transitionDuration ??
defaultTransitionDuration),
reverseTransitionDuration: transition == TransitionType.none
? Duration.zero
: (transitionDuration ?? route?.transitionDuration)!,
: (transitionDuration ??
route?.transitionDuration ??
defaultTransitionDuration),
transitionsBuilder: transition == TransitionType.none
? (_, __, ___, child) => child
: routeTransitionsBuilder!,
Expand Down
35 changes: 15 additions & 20 deletions lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class RouteTreeNode {
// properties
String part;
RouteTreeNodeType? type;
List<AppRoute>? routes = <AppRoute>[];
List<RouteTreeNode>? nodes = <RouteTreeNode>[];
List<AppRoute> routes = <AppRoute>[];
List<RouteTreeNode> nodes = <RouteTreeNode>[];
RouteTreeNode? parent;

bool isParameter() {
Expand Down Expand Up @@ -97,15 +97,11 @@ class RouteTree {
if (parent == null) {
_nodes.add(node);
} else {
parent.nodes!.add(node);
parent.nodes.add(node);
}
}
if (i == pathComponents.length - 1) {
if (node.routes == null) {
node.routes = [route];
} else {
node.routes!.add(route);
}
node.routes.add(route);
}
parent = node;
}
Expand Down Expand Up @@ -149,9 +145,7 @@ class RouteTree {
match.parameters.addAll(queryMap);
}
currentMatches[node] = match;
if (node.nodes != null) {
nextNodes.addAll(node.nodes!);
}
nextNodes.addAll(node.nodes);
}
}
nodeMatches = currentMatches;
Expand All @@ -161,11 +155,11 @@ class RouteTree {
}
}
List<RouteTreeNodeMatch> matches = nodeMatches.values.toList();
if (matches.length > 0) {
if (matches.isNotEmpty) {
RouteTreeNodeMatch match = matches.first;
RouteTreeNode? nodeToUse = match.node;
final routes = nodeToUse.routes;
if (routes != null && routes.length > 0) {
if (routes.isNotEmpty) {
AppRouteMatch routeMatch = AppRouteMatch(routes[0]);
routeMatch.parameters = match.parameters;
return routeMatch;
Expand All @@ -179,14 +173,14 @@ class RouteTree {
}

void _printSubTree({RouteTreeNode? parent, int level = 0}) {
List<RouteTreeNode>? nodes = parent != null ? parent.nodes : _nodes;
for (RouteTreeNode node in nodes!) {
List<RouteTreeNode> nodes = parent != null ? parent.nodes : _nodes;
for (RouteTreeNode node in nodes) {
String indent = "";
for (int i = 0; i < level; i++) {
indent += " ";
}
print("$indent${node.part}: total routes=${node.routes!.length}");
if (node.nodes != null && node.nodes!.length > 0) {
print("$indent${node.part}: total routes=${node.routes.length}");
if (node.nodes.isNotEmpty) {
_printSubTree(parent: node, level: level + 1);
}
}
Expand All @@ -196,7 +190,7 @@ class RouteTree {
List<RouteTreeNode> nodes = _nodes;
if (parent != null) {
// search parent for sub-node matches
nodes = parent.nodes!;
nodes = parent.nodes;
}
for (RouteTreeNode node in nodes) {
if (node.part == component) {
Expand All @@ -220,13 +214,14 @@ class RouteTree {
}

Map<String, List<String>> parseQueryString(String query) {
var search = RegExp('([^&=]+)=?([^&]*)');
var params = Map<String, List<String>>();
final search = RegExp('([^&=]+)=?([^&]*)');
final params = Map<String, List<String>>();
if (query.startsWith('?')) query = query.substring(1);
decode(String s) => Uri.decodeComponent(s.replaceAll('+', ' '));
for (Match match in search.allMatches(query)) {
String key = decode(match.group(1)!);
String value = decode(match.group(2)!);

if (params.containsKey(key)) {
params[key]!.add(value);
} else {
Expand Down

0 comments on commit a86e8b4

Please sign in to comment.