浏览代码

nnbd second pass

lukepighetti 4 年之前
父节点
当前提交
4f53b1a113

+ 1 - 0
example/lib/config/route_handlers.dart

@@ -61,6 +61,7 @@ var demoFunctionHandler = Handler(
           );
         },
       );
+      return;
     });
 
 /// Handles deep links into the app

+ 1 - 0
example/lib/config/routes.dart

@@ -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);

+ 1 - 1
example/pubspec.lock

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

+ 11 - 11
lib/src/common.dart

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

+ 2 - 2
lib/src/extensions.dart

@@ -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;
 }

+ 12 - 9
lib/src/fluro_router.dart

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

+ 9 - 13
lib/src/tree.dart

@@ -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]
@@ -39,7 +39,7 @@ class RouteTreeNodeMatch {
   }
 
   // properties
-  RouteTreeNode? node;
+  RouteTreeNode node;
   Map<String, List<String>> parameters = <String, List<String>>{};
 }
 
@@ -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>[];
@@ -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) {
@@ -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!);
@@ -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;
@@ -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

+ 1 - 1
pubspec.lock

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

+ 6 - 11
test/parser_test.dart

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