Home > Software engineering >  How do I compare two app versions in flutter?
How do I compare two app versions in flutter?

Time:09-28

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 ?

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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
  • Related