version top-level property

String version
final

Implementation

final version = () {
  final pubCachePath = desktopPubCachePath;
  final directory = Directory(pubCachePath);

  // Filter for relevant directories and extract versions
  final versions = directory
      .listSync()
      .whereType<Directory>() // Filter out non-directory entries
      .map((directory) {
        final name = path.basename(directory.path);
        final match = RegExp('$_libraryName-(.*)').firstMatch(name);
        return match != null
            ? match.group(1)!
            : '-1'; // Use null-aware operator and assert non-null group
      })
      .where((version) => version != '-1') // Filter out non-matching versions
      .toList();

  versions.sort(); // Sort versions in ascending order

  return versions.isEmpty
      ? throw StateError('Package $_libraryName not found in $pubCachePath. Have you run `dart pub install $_libraryName`?')
      : versions.last; // Handle the case where no matching versions are found
}();