Browse Source

null-safe nits and improvements

lukepighetti 4 years ago
parent
commit
ebecb8f19f
3 changed files with 35 additions and 35 deletions
  1. 2 2
      lib/src/common.dart
  2. 18 13
      lib/src/fluro_router.dart
  3. 15 20
      lib/src/tree.dart

+ 2 - 2
lib/src/common.dart

@@ -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 {

+ 18 - 13
lib/src/fluro_router.dart

@@ -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,
@@ -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));
       }
@@ -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].
@@ -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!);
@@ -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) {
@@ -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!,

+ 15 - 20
lib/src/tree.dart

@@ -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() {
@@ -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;
     }
@@ -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;
@@ -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;
@@ -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);
       }
     }
@@ -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) {
@@ -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 {