applyPreview method

  1. @visibleForTesting
void applyPreview(
  1. String route, {
  2. String data = '',
  3. String stack = '',
})

Makes route the preview route, as PREVIEW_ROUTE does.

route may include a query string. data is decoded as JSON when it can be, and passed as a string otherwise. stack is a comma-separated list of routes to open underneath.

Implementation

@visibleForTesting
void applyPreview(String route, {String data = '', String stack = ''}) {
  Uri uri = Uri.parse(route);
  NyRouterRoute? nyRoute = _routeNameMappings[uri.path];
  if (nyRoute == null) {
    NyLogger.warning(
      'PREVIEW_ROUTE is not a registered route.',
      context: {'route': uri.path},
    );
    return;
  }

  dynamic previewData;
  if (data.isNotEmpty) {
    try {
      previewData = jsonDecode(data);
    } on FormatException {
      previewData = data;
    }
  }

  nyRoute.previewRoute(
    data: previewData,
    queryParameters: uri.queryParameters.isEmpty ? null : uri.queryParameters,
    stack: stack
        .split(',')
        .map((name) => name.trim())
        .where((name) => name.isNotEmpty)
        .toList(),
  );
}