How do I compare two app versions in flutter?

Viewed 3039

I was trying to compare two different app version in flutter, But I am not able to assign , App versions in a variable.

var a = 1.0.0;
var b = 1.0.1;

But the values not getting assigned in variables.

I think 1.0.0 is not float value, so how to assign that value and compare them ?

8 Answers

Since the version and revision numbers can go more than 9. We often have version numbers like "1.13.11", "211.74.23" etc. . A solution is to split and compare the individual numbers.

 String a = "1.39.2";
 String b = "1.38.14"; 
 bool isVersionGreaterThan(String newVersion, String currentVersion){
   List<String> currentV = currentVersion.split(".");
   List<String> newV = newVersion.split(".");
   bool a = false;
   for (var i = 0 ; i <= 2; i++){
     a = int.parse(newV[i]) > int.parse(currentV[i]);
     if(int.parse(newV[i]) != int.parse(currentV[i])) break;
   }
   return a;
 }
  print(isVersionGreaterThan(a,b));

A cool solution, is to convert each version to an exponential integer, so we could compare them simply as integers!

void main() {
  String v1 = '1.2.3', v2 = '1.2.11';
  int v1Number = getExtendedVersionNumber(v1); // return 10020003
  int v2Number = getExtendedVersionNumber(v2); // return 10020011
  print(v1Number >= v2Number);
}

int getExtendedVersionNumber(String version) {
  List versionCells = version.split('.');
  versionCells = versionCells.map((i) => int.parse(i)).toList();
  return versionCells[0] * 100000 + versionCells[1] * 1000 + versionCells[2];
}

Checkout this running example on dartpad.

Use version package from pub.dev

a bit about this package

A dart library providing a Version object for comparing and incrementing version numbers in compliance with the Semantic Versioning spec at [http://semver.org/][2]

you can use this code to compare and perform your task

Version latestVersion = Version.parse("1.5.1");

if (latestVersion > Version.parse(1.3.0))
    _newUpdateDialog(); // your function here

according to this package on pub.dev: package_info_plus

import 'package:package_info_plus/package_info_plus.dart';

PackageInfo packageInfo = await PackageInfo.fromPlatform();

String version = packageInfo.version;

the version is in String format, so you can split this string by '.' and get individual int and compare them individually, heres example if you need

Another easy approach would be to simply use the Version plugin which helps compare versions. It also handles many edge cases.

Use it like this:-

import 'package:version/version.dart';

void main() {

  Version currentVersion = Version.parse("10.1.0");
  // Could use like this also
  // Version currentVersion = Version(10, 1, 0);
  Version latestVersion = Version.parse("9.10.0");

  // Note: this test will print "Current version higher than latest version",
  // as the provided latest version is smaller than the current version.
  if (latestVersion > currentVersion) {
    debugPrint("Current version smaller than latest version");
    // Should update app to latest version.
  } else if (latestVersion == currentVersion) {
    debugPrint("Current version same as latest version");
    // App already using latest version.
  } else {
    debugPrint("Current version higher than latest version");
    // App using newer version than provided latest version.
  }

  //For pre-release version comparing
  Version betaVersion = Version(9, 10, 0, preRelease: ["beta"]);
  // Note: this test will return false, as pre-release versions are considered
  // lesser then a non-pre-release version that otherwise has the same numbers.
  if (betaVersion > latestVersion) {
    print("More recent beta available");
  }
}

Update:-

Just letting others know that I'm using this approach in my production app for quite a long time. And I have pushed many updates through the GitHub releases where my app uses this package to compare the current and new versions over GitHub releases. And TBH I never faced any issues. Really great package.

@Asquere answer is right. However if you want it to work with both x.x.x.x... number and when comparing

Future<bool> _isHigherThanCurrentVersion(String newVersion, String currentVersion) async {
  var isHigher = false;

  try {
    final currentVSegments = currentVersion.split('.');
    final newVSegments = newVersion.split('.');
    final maxLength = max(currentVSegments.length, newVSegments.length);
    for (var i = 0; i < maxLength; i++) {
      final newVSegment =
          i < newVSegments.length ? int.parse(newVSegments[i]) : 0;
      final currentVSegment =
          i < currentVSegments.length ? int.parse(currentVSegments[i]) : 0;
      isHigher = newVSegment > currentVSegment;
      if (newVSegment != currentVSegment) {
        break;
      }
    }
  } on Exception catch (e) {
    // this should silently fail
    // we will return false in case of parsing error
  }
  return isHigher;
}

Ok, I got temporary solution,

void main() {
  String a = "1.0.0";
  String b = "1.0.1";

  int intA = int.parse(a.replaceAll(".",""));
  int intB = int.parse(b.replaceAll(".",""));

  if(intB > intA){
    print("New update available");
  }  
} 

you can convert the app version into int

int.parse("243.00.452".replaceAll(".",""))
Related