compareVersions static method
Compares two semantic versions.
v1 - The first version to compare
v2 - The second version to compare
Returns true if v1 is newer than v2.
Implementation
static bool compareVersions(String v1, String v2) {
final parts1 = v1.split('.');
final parts2 = v2.split('.');
final minLength =
parts1.length < parts2.length ? parts1.length : parts2.length;
for (int i = 0; i < minLength; i++) {
// Remove non-numeric parts (e.g., "-beta")
final num1Part = parts1[i].split('-')[0];
final num2Part = parts2[i].split('-')[0];
final num1 = int.tryParse(num1Part) ?? 0;
final num2 = int.tryParse(num2Part) ?? 0;
if (num1 > num2) return true;
if (num1 < num2) return false;
}
// Check if it's a pre-release
final isPrerelease1 = v1.contains('-');
final isPrerelease2 = v2.contains('-');
// Official releases are always newer than pre-releases
if (!isPrerelease1 && isPrerelease2) return true;
if (isPrerelease1 && !isPrerelease2) return false;
// If the version numbers are the same, consider the one with more segments as newer
return parts1.length > parts2.length;
}