getResolvedPackageVersion static method

String? getResolvedPackageVersion({
  1. required String packageDirPath,
  2. required String packageName,
})

The version of packageName recorded in the project's pubspec.lock, or null when there is no lock file or no entry for the package in it.

Implementation

static String? getResolvedPackageVersion({
  required String packageDirPath,
  required String packageName,
}) {
  try {
    final File lockFile = File('$packageDirPath$kPubspecLockPath');
    if (!lockFile.existsSync()) {
      return null;
    }
    final dynamic lock = loadYaml(lockFile.readAsStringSync());
    final dynamic package = lock?[kPackages]?[packageName];
    final dynamic version = package?[kVersion];
    return version?.toString();
  } catch (e) {
    // A damaged lock file is not worth failing over: the constraint below
    // still gives a usable answer for an exact pin.
    return null;
  }
}