packageSupportsPlatform function
Fetches the supported platforms for a given package from its pub.dev page. Returns a list of supported platforms in uppercase.
Implementation
Future<List<String>> packageSupportsPlatform(String packageName) async {
// Fetch the package's page from pub.dev.
var response = await http
.get(Uri.parse("https://pub.dev/packages/$packageName"))
.timeout(
const Duration(seconds: 3),
onTimeout: () {
print("timeout");
throw Exception("Timeout");
},
);
// If the response is successful (status code 200), parse the HTML to extract platform information.
if (response.statusCode == 200) {
var document = parse(response.body);
List<Element> tagBadgeDiv = document.querySelectorAll('.-pub-tag-badge');
// Check if the tag badge div exists and extract the supported platforms from it.
if (tagBadgeDiv.isNotEmpty) {
List<Element> platformElements =
tagBadgeDiv[1].querySelectorAll('.tag-badge-sub');
List<String> supportedPlatforms = [];
for (Element platformElement in platformElements) {
supportedPlatforms.add(platformElement.text.toUpperCase());
}
return supportedPlatforms;
}
}
// If the package page doesn't provide platform information or there was an error, return an empty list.
return [];
}