AssetId.resolve constructor

AssetId.resolve(
  1. Uri uri, {
  2. AssetId? from,
})

Creates an AssetId from a uri.

The uri can be relative, in which case it will be resolved relative to from; if uri is relative and from is not specified, an ArgumentError is thrown.

Or, uri can have the scheme package or asset.

A package uri has the form package:$package/$path and references the specified path within the lib/ directory of the specified package, just like a package URI in Dart source code.

An asset uri has the form asset:$package/$path and references the specified path within the root of the specified package.

If uri has any other scheme then UnsupportedError is thrown.

Implementation

factory AssetId.resolve(Uri uri, {AssetId? from}) {
  if (from == null && !uri.hasScheme) {
    throw ArgumentError.value(
      from,
      'from',
      'An AssetId `from` must be specified to resolve a relative URI.',
    );
  }
  final resolved = uri.hasScheme ? uri : from!.uri.resolveUri(uri);
  if (resolved.scheme == 'package') {
    return AssetId(
      resolved.pathSegments.first,
      p.url.join('lib', p.url.joinAll(resolved.pathSegments.skip(1))),
    );
  } else if (resolved.scheme == 'asset') {
    return AssetId(
      resolved.pathSegments.first,
      p.url.joinAll(resolved.pathSegments.skip(1)),
    );
  }
  throw UnsupportedError(
    'Cannot resolve $uri; only "package" and "asset" schemes supported.',
  );
}