parseRouteInformationWithDependencies method

  1. @override
Future<RouteMatchList> parseRouteInformationWithDependencies(
  1. RouteInformation routeInformation,
  2. BuildContext context
)
override

Called by the Router. The

Implementation

@override
Future<RouteMatchList> parseRouteInformationWithDependencies(
  RouteInformation routeInformation,
  BuildContext context,
) {
  assert(routeInformation.state != null);
  final Object state = routeInformation.state!;

  if (state is! RouteInformationState) {
    // This is a result of browser backward/forward button or state
    // restoration. In this case, the route match list is already stored in
    // the state.
    final RouteMatchList matchList =
        _routeMatchListCodec.decode(state as Map<Object?, Object?>);
    return debugParserFuture = _redirect(context, matchList)
        .then<RouteMatchList>((RouteMatchList value) {
      if (value.isError && onParserException != null) {
        // TODO(chunhtai): Figure out what to return if context is invalid.
        // ignore: use_build_context_synchronously
        return onParserException!(context, value);
      }
      return value;
    });
  }

  late final RouteMatchList initialMatches;
  if (routeInformation.uri.hasEmptyPath) {
    String newUri = '${routeInformation.uri.origin}/';
    if (routeInformation.uri.hasQuery) {
      newUri += '?${routeInformation.uri.query}';
    }
    if (routeInformation.uri.hasFragment) {
      newUri += '#${routeInformation.uri.fragment}';
    }
    initialMatches = configuration.findMatch(
      newUri,
      extra: state.extra,
    );
  } else {
    initialMatches = configuration.findMatch(
      routeInformation.uri.toString(),
      extra: state.extra,
    );
  }
  if (initialMatches.isError) {
    log('No initial matches: ${routeInformation.uri.path}');
  }

  return debugParserFuture = _redirect(
    context,
    initialMatches,
  ).then<RouteMatchList>((RouteMatchList matchList) {
    if (matchList.isError && onParserException != null) {
      // TODO(chunhtai): Figure out what to return if context is invalid.
      // ignore: use_build_context_synchronously
      return onParserException!(context, matchList);
    }

    assert(() {
      if (matchList.isNotEmpty) {
        assert(!matchList.last.route.redirectOnly,
            'A redirect-only route must redirect to location different from itself.\n The offending route: ${matchList.last.route}');
      }
      return true;
    }());
    return _updateRouteMatchList(
      matchList,
      baseRouteMatchList: state.baseRouteMatchList,
      completer: state.completer,
      type: state.type,
    );
  });
}