findPackageConfigUri function

Future<PackageConfig?> findPackageConfigUri(
  1. Uri location, {
  2. bool recurse = true,
  3. int minVersion = 1,
  4. Future<Uint8List?> loader(
    1. Uri uri
    )?,
  5. void onError(
    1. Object error
    )?,
})

Finds a package configuration relative to location.

If location contains a package configuration, either a .dart_tool/package_config.json file or, if not, a .packages, then that file is loaded. The location URI must not be a package: URI. It should be a hierarchical URI which is supported by loader.

If no file is found in the current directory, then the parent directories are checked recursively, all the way to the root directory, to check if those contains a package configuration. If recurse is set to false, this parent directory check is not performed.

If loader is provided, URIs are loaded using that function. The future returned by the loader must complete with a Uint8List containing the entire file content, or with null if the file does not exist. The loader may throw at its own discretion, for situations where it determines that an error might be need user attention, but it is always allowed to return null. This function makes no attempt to catch such errors.

If no loader is supplied, a default loader is used which only accepts file:, http: and https: URIs, and which uses the platform file system and HTTP requests to fetch file content. The default loader never throws because of an I/O issue, as long as the location URIs are valid. As such, it does not distinguish between a file not existing, and it being temporarily locked or unreachable.

If onError is provided, the configuration file parsing will report errors by calling that function, and then try to recover. The returned package configuration is a best effort attempt to create a valid configuration from the invalid configuration file. If no onError is provided, errors are thrown immediately.

If minVersion is set to something greater than its default, any lower-version configuration files are ignored in the search.

Returns null if no configuration file is found.

Implementation

Future<PackageConfig?> findPackageConfigUri(Uri location,
    {bool recurse = true,
    int minVersion = 1,
    Future<Uint8List?> Function(Uri uri)? loader,
    void Function(Object error)? onError}) {
  if (minVersion > PackageConfig.maxVersion) {
    throw ArgumentError.value(minVersion, 'minVersion',
        'Maximum known version is ${PackageConfig.maxVersion}');
  }
  return discover.findPackageConfigUri(
      location, minVersion, loader, onError ?? throwError, recurse);
}