buildPage method

Page<Object?> buildPage({
  1. required BuildContext context,
  2. required AppContext app,
  3. required RouteNavigator router,
  4. required RouteMatches matches,
  5. required RouteLoadResult? loadResult,
  6. required Object? pageScope,
  7. required PopInvokedWithResultCallback<Object?> onPopInvoked,
})

Builds this route's Flutter page for one navigation snapshot.

Implementation

Page<Object?> buildPage({
  required BuildContext context,
  required AppContext app,
  required RouteNavigator router,
  required RouteMatches matches,
  required RouteLoadResult? loadResult,
  required Object? pageScope,
  required PopInvokedWithResultCallback<Object?> onPopInvoked,
}) {
  final match = matches.match(this);
  if (match == null) {
    throw StateError('Page route is not part of the active route branch.');
  }
  RouteContext<P, S> routeContext(BuildContext buildContext) =>
      RouteContext<P, S>(
        buildContext: buildContext,
        app: app,
        router: router,
        params: match.params,
        search: match.search,
        location: matches.location,
        matches: matches,
      );
  RoutePageContext<P, S, D> readyContext(BuildContext buildContext) =>
      RoutePageContext<P, S, D>(
        buildContext: buildContext,
        app: app,
        router: router,
        params: match.params,
        search: match.search,
        location: matches.location,
        matches: matches,
        data: loadResult!.data as D,
      );
  RoutePageState<P, S, D> readyState() => RoutePageState<P, S, D>(
    app: app,
    router: router,
    params: match.params,
    search: match.search,
    location: matches.location,
    matches: matches,
    data: loadResult!.data as D,
  );
  final key = ValueKey<_RoutePageIdentity>(
    _RoutePageIdentity(identity, match.params, pageScope),
  );

  if (loadResult == null) {
    return _WidgetPage(
      key: key,
      name: matches.location.toString(),
      onPopInvoked: onPopInvoked,
      child: Builder(
        builder: (context) =>
            pending?.call(routeContext(context)) ?? const SizedBox.shrink(),
      ),
    );
  }

  final failure = loadResult.error;
  if (failure != null) {
    return _WidgetPage(
      key: key,
      name: matches.location.toString(),
      onPopInvoked: onPopInvoked,
      child: Builder(
        builder: (context) {
          final current = routeContext(context);
          if (failure is RouteNotFoundException && notFound != null) {
            return notFound!(current);
          }
          if (error != null) return error!(current, failure);
          return ErrorWidget.withDetails(
            message: failure.toString(),
            error: FlutterError(failure.toString()),
          );
        },
      ),
    );
  }

  final pageBuilder = page;
  if (pageBuilder != null) {
    final settings = RoutePageSettings(
      key: key,
      name: matches.location.toString(),
      onPopInvoked: onPopInvoked,
    );
    final result = pageBuilder(readyState(), settings);
    if (result.key != key) {
      throw StateError('A custom route Page must use settings.key.');
    }
    if (!identical(result.onPopInvoked, onPopInvoked)) {
      throw StateError('A custom route Page must use settings.onPopInvoked.');
    }
    return result;
  }
  return _WidgetPage(
    key: key,
    name: matches.location.toString(),
    onPopInvoked: onPopInvoked,
    child: Builder(builder: (context) => build!(readyContext(context))),
  );
}