loadPackageConfigUri function

Future<PackageConfig> loadPackageConfigUri(
  1. Uri file, {
  2. Future<Uint8List?> loader(
    1. Uri uri
    )?,
  3. bool preferNewest = true,
  4. void onError(
    1. Object error
    )?,
})

Reads a specific package configuration URI.

The file of the URI must exist and be readable. It must be either a valid package_config.json file or a valid .packages file. It is considered a package_config.json file if its first non-whitespace character is a {.

If preferNewest is true, the default, and the file is a .packages file, as determined by its file name being .packages, first checks if there is a .dart_tool/package_config.json file next to the original file, and if so, loads that instead. The file must not be a package: URI. If preferNewest is set to false, a directly specified .packages file is loaded even if there is an available package_config.json file. The caller can determine this from the PackageConfig.version being 1 and look for a package_config.json file themselves.

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 encoded as UTF-8, 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. As such, it may throw any error that loader throws.

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.

Implementation

Future<PackageConfig> loadPackageConfigUri(Uri file,
        {Future<Uint8List?> Function(Uri uri)? loader,
        bool preferNewest = true,
        void Function(Object error)? onError}) =>
    readAnyConfigFileUri(file, loader, onError ?? throwError, preferNewest);