loadPackageConfig function

Future<PackageConfig> loadPackageConfig(
  1. File file, {
  2. bool preferNewest = true,
  3. void onError(
    1. Object error
    )?,
})

Reads a specific package configuration file.

The file 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 character is a {.

If the file is a .packages file (the file name is .packages) and preferNewest is true, the default, also checks if there is a .dart_tool/package_config.json file next to the original file, and if so, loads that instead. 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 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> loadPackageConfig(File file,
        {bool preferNewest = true, void Function(Object error)? onError}) =>
    readAnyConfigFile(file, preferNewest, onError ?? throwError);