importCanonical method Null safety

Future<Stylesheet?> importCanonical(
  1. AsyncImporter importer,
  2. Uri canonicalUrl,
  3. {Uri? originalUrl,
  4. bool quiet = false}
)

Tries to load the canonicalized canonicalUrl using importer.

If importer can import canonicalUrl, returns the imported Stylesheet. Otherwise returns null.

If passed, the originalUrl represents the URL that was canonicalized into canonicalUrl. It's used to resolve a relative canonical URL, which importers may return for legacy reasons.

If quiet is true, this will disable logging warnings when parsing the newly imported stylesheet.

Caches the result of the import and uses cached results if possible.

Implementation

Future<Stylesheet?> importCanonical(AsyncImporter importer, Uri canonicalUrl,
    {Uri? originalUrl, bool quiet = false}) async {
  return await putIfAbsentAsync(_importCache, canonicalUrl, () async {
    var result = await importer.load(canonicalUrl);
    if (result == null) return null;

    _resultsCache[canonicalUrl] = result;
    return Stylesheet.parse(result.contents, result.syntax,
        // For backwards-compatibility, relative canonical URLs are resolved
        // relative to [originalUrl].
        url: originalUrl == null
            ? canonicalUrl
            : originalUrl.resolveUri(canonicalUrl),
        logger: quiet ? Logger.quiet : _logger);
  });
}