listPackages method
Future<List<Package> >
listPackages({
- ListPackageFilter filter = const ListPackageFilter(),
- ListPackageDisplayOptions displayOptions = const ListPackageDisplayOptions(),
- String? nameFilter,
- bool debug = false,
Get the list of packages installed on the device.
The filter parameter can be used to filter the list of packages.
The displayOptions parameter can be used to specify the display options for the list of packages.
The nameFilter parameter can be used to filter the list of packages by name.
Throws an exception if the package manager command fails.
Example:
final packages = await pm.listPackages();
Implementation
Future<List<Package>> listPackages({
ListPackageFilter filter = const ListPackageFilter(),
ListPackageDisplayOptions displayOptions = const ListPackageDisplayOptions(),
String? nameFilter,
bool debug = false,
}) async {
final args = ['pm', 'list', 'packages'];
args.addAll(filter.toArgs());
args.addAll(displayOptions.toArgs());
if (nameFilter != null) {
args.add(nameFilter);
}
final lines = await _shell.exec(args, debug: debug);
final result = <Package>[];
lines.stdout.toString().split('\n').forEach((line) {
final match = _kListPackagesRegExp.firstMatch(line);
if (match != null && match.groupCount == 9) {
final file = match.namedGroup('file');
final name = match.namedGroup('name');
int? versionCode;
int? uid;
if (match.group(5) == 'versionCode') {
versionCode = int.parse(match.group(6)!);
uid = int.parse(match.group(9)!);
} else if (match.group(5) == 'uid') {
uid = int.parse(match.group(6)!);
}
result.add(Package(packageName: name!, path: file, versionCode: versionCode, uid: uid));
}
});
return result;
}