$route<T extends GoRouteData> static method

GoRoute $route<T extends GoRouteData>({
  1. required String path,
  2. String? name,
  3. required T factory(
    1. GoRouterState
    ),
  4. GlobalKey<NavigatorState>? parentNavigatorKey,
  5. List<RouteBase> routes = const <RouteBase>[],
})

A helper function used by generated code.

Should not be used directly.

Implementation

static GoRoute $route<T extends GoRouteData>({
  required String path,
  String? name,
  required T Function(GoRouterState) factory,
  GlobalKey<NavigatorState>? parentNavigatorKey,
  List<RouteBase> routes = const <RouteBase>[],
}) {
  T factoryImpl(GoRouterState state) {
    final Object? extra = state.extra;

    // If the "extra" value is of type `T` then we know it's the source
    // instance of `GoRouteData`, so it doesn't need to be recreated.
    if (extra is T) {
      return extra;
    }

    return (_stateObjectExpando[state] ??= factory(state)) as T;
  }

  Widget builder(BuildContext context, GoRouterState state) =>
      factoryImpl(state).build(context, state);

  Page<void> pageBuilder(BuildContext context, GoRouterState state) =>
      factoryImpl(state).buildPage(context, state);

  FutureOr<String?> redirect(BuildContext context, GoRouterState state) =>
      factoryImpl(state).redirect(context, state);

  FutureOr<bool> onExit(BuildContext context, GoRouterState state) =>
      factoryImpl(state).onExit(context, state);

  return GoRoute(
    path: path,
    name: name,
    builder: builder,
    pageBuilder: pageBuilder,
    redirect: redirect,
    routes: routes,
    parentNavigatorKey: parentNavigatorKey,
    onExit: onExit,
  );
}