matchRoute method Null safety

RouteMatch matchRoute(
  1. BuildContext? buildContext,
  2. String? path,
  3. {RouteSettings? routeSettings,
  4. TransitionType? transitionType,
  5. Duration? transitionDuration,
  6. RouteTransitionsBuilder? transitionsBuilder,
  7. bool maintainState = true}
)

Attempt to match a route to the provided path.

Implementation

RouteMatch matchRoute(BuildContext? buildContext, String? path,
    {RouteSettings? routeSettings,
    TransitionType? transitionType,
    Duration? transitionDuration,
    RouteTransitionsBuilder? transitionsBuilder,
    bool maintainState = true}) {
  RouteSettings settingsToUse = routeSettings ?? RouteSettings(name: path);

  if (settingsToUse.name == null) {
    settingsToUse = settingsToUse.copyWith(name: path);
  }
  AppRouteMatch? match = _routeTree.matchRoute(path!);
  AppRoute? route = match?.route;

  if (transitionDuration == null && route?.transitionDuration != null) {
    transitionDuration = route?.transitionDuration;
  }

  Handler handler = (route != null ? route.handler : notFoundHandler);
  TransitionType? transition = transitionType;
  if (transitionType == null) {
    transition = route != null ? route.transitionType : TransitionType.native;
  }
  if (route == null && notFoundHandler == null) {
    return RouteMatch(
        matchType: RouteMatchType.noMatch,
        errorMessage: "No matching route was found");
  }
  Map<String, List<String>> parameters =
      match?.parameters ?? <String, List<String>>{};
  if (handler.type == HandlerType.function) {
    handler.handlerFunc(buildContext, parameters);
    return RouteMatch(matchType: RouteMatchType.nonVisual);
  }

  RouteCreator creator =
      (RouteSettings? routeSettings, Map<String, List<String>> parameters) {
    bool isNativeTransition = (transition == TransitionType.native ||
        transition == TransitionType.nativeModal);
    if (isNativeTransition) {
      return MaterialPageRoute<dynamic>(
          settings: routeSettings,
          fullscreenDialog: transition == TransitionType.nativeModal,
          maintainState: maintainState,
          builder: (BuildContext context) {
            return handler.handlerFunc(context, parameters) ??
                SizedBox.shrink();
          });
    } else if (transition == TransitionType.material ||
        transition == TransitionType.materialFullScreenDialog) {
      return MaterialPageRoute<dynamic>(
          settings: routeSettings,
          fullscreenDialog:
              transition == TransitionType.materialFullScreenDialog,
          maintainState: maintainState,
          builder: (BuildContext context) {
            return handler.handlerFunc(context, parameters) ??
                SizedBox.shrink();
          });
    } else if (transition == TransitionType.cupertino ||
        transition == TransitionType.cupertinoFullScreenDialog) {
      return CupertinoPageRoute<dynamic>(
          settings: routeSettings,
          fullscreenDialog:
              transition == TransitionType.cupertinoFullScreenDialog,
          maintainState: maintainState,
          builder: (BuildContext context) {
            return handler.handlerFunc(context, parameters) ??
                SizedBox.shrink();
          });
    } else {
      RouteTransitionsBuilder? routeTransitionsBuilder;

      if (transition == TransitionType.custom) {
        routeTransitionsBuilder =
            transitionsBuilder ?? route?.transitionBuilder;
      } else {
        routeTransitionsBuilder = _standardTransitionsBuilder(transition);
      }

      return PageRouteBuilder<dynamic>(
        settings: routeSettings,
        maintainState: maintainState,
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
          return handler.handlerFunc(context, parameters) ??
              SizedBox.shrink();
        },
        transitionDuration: transition == TransitionType.none
            ? Duration.zero
            : (transitionDuration ??
                route?.transitionDuration ??
                defaultTransitionDuration),
        reverseTransitionDuration: transition == TransitionType.none
            ? Duration.zero
            : (transitionDuration ??
                route?.transitionDuration ??
                defaultTransitionDuration),
        transitionsBuilder: transition == TransitionType.none
            ? (_, __, ___, child) => child
            : routeTransitionsBuilder!,
      );
    }
  };
  return RouteMatch(
    matchType: RouteMatchType.visual,
    route: creator(settingsToUse, parameters),
  );
}