compareVersion method

int compareVersion(
  1. String other
)

Returns -1, 0, 1 if this version is less than, equal to, or greater than the other

Implementation

int compareVersion(String other) {
  final regExpV = RegExp(r'(\d+)\.(\d+)\.(\d+)');
  final thisV = regExpV.firstMatch(this)?.group(0) ?? "";
  final otherV = regExpV.firstMatch(other)?.group(0) ?? "";
  if (thisV.isEmpty && otherV.isEmpty) {
    return 0;
  } else if (thisV.isEmpty) {
    return -1;
  } else if (otherV.isEmpty) {
    return 1;
  }
  final List<String> thisParts = thisV.split(".");
  final List<String> otherParts = otherV.split(".");

  while (thisParts.length < otherParts.length) {
    thisParts.add("0");
  }
  while (otherParts.length < thisParts.length) {
    otherParts.add("0");
  }
  try {
    for (int i = 0; i < thisParts.length; i++) {
      final int thisPart = int.parse(thisParts[i]);
      final int otherPart = int.parse(otherParts[i]);
      if (thisPart < otherPart) {
        return -1;
      } else if (thisPart > otherPart) {
        return 1;
      }
    }
  } catch (e) {
    print("Error parsing version: $e");
  }
  return 0;
}