Issue
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
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 version1Number = getExtendedVersionNumber(v1); // return 102003
int version2Number = getExtendedVersionNumber(v2); // return 102011
print(version1Number == version2Number);
print(version1Number >= version2Number);
print(version1Number < version2Number);
}
int getExtendedVersionNumber(String version) {
// Note that if you want to support bigger versions than 99,
// increase the returned versionCells multipliers
List versionCells = version.split('.');
versionCells = versionCells.map((i) => int.parse(i)).toList();
return versionCells[0] * 10000 + versionCells[1] * 100 + versionCells[2];
}
Checkout this running example on dartpad.
Answered By - genericUser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.