getDartInstalledPackages function

Future<List<DartInstalledCliPackage>> getDartInstalledPackages({
  1. bool? all,
  2. bool? force,
  3. bool? verbose,
})

Get the packages installed with dart install (with a local cache, per isolate).

all includes the non active ones (dart installed --all), a package can then be listed more than once (one per version).

force lists them again and refreshes the cache, i.e. when another process might have installed or uninstalled a package.

Requires Dart 3.10+.

Implementation

Future<List<DartInstalledCliPackage>> getDartInstalledPackages({
  bool? all,
  bool? force,
  bool? verbose,
}) async {
  all ??= false;
  if (force ?? false) {
    _dartInstalledPackages.clear();
  }
  var future = _dartInstalledPackages.putIfAbsent(
    all,
    () => _listDartInstalledPackages(all: all!, verbose: verbose ?? false),
  );
  try {
    // A copy, the cache is not modified by callers.
    return (await future).toList();
  } catch (_) {
    // Don't cache a failure.
    if (identical(_dartInstalledPackages[all], future)) {
      // Already awaited, its error is rethrown.
      _dartInstalledPackages.remove(all)?.ignore();
    }
    rethrow;
  }
}