'How do I compare two app versions in flutter?

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 ?



Solution 1:[1]

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.

Solution 2:[2]

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));

Solution 3:[3]

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

Solution 4:[4]

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

Solution 5:[5]

u mean , When a newer app version is available in the app store, a simple alert prompt or card is displayed. With today's modern app stores, there is little need to persuade users to upgrade because most are already using the auto upgrade feature.

Try to use : https://pub.dev/packages/upgrader

Solution 6:[6]

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");
  }
}

Solution 7:[7]

you can convert the app version into int

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

Solution 8:[8]

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");
  }  
} 

Solution 9:[9]

Try this , https://pub.dev/packages/upgrader it will help you to force update the app

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ruchit
Solution 4 Vicky Salunkhe
Solution 5
Solution 6 Deepanshu
Solution 7 Lalit Rawat
Solution 8 Coder Addy
Solution 9