简体   繁体   English

如何比较 Flutter 中的两个应用程序版本?

[英]How to compare two app versions in Flutter?

I was trying to compare the two app versions in Flutter:我试图比较 Flutter 中的两个应用程序版本:

final v1 = "1.0.0";
final v2 = "1.0.1";

How do I state which version is bigger than the other?如何说明哪个版本比另一个版本大?

Since the version and revision numbers can go more than 9. We often have version numbers like "1.13.11", "211.74.23" etc. .由于版本号和修订号可以超过 9。我们经常有“1.13.11”、“211.74.23”等版本号。 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.dartpad上查看这个正在运行的示例

Use version package from pub.dev使用来自 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根据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并获取单独的 int 并单独比较它们,如果需要,这里是示例

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尝试使用: https ://pub.dev/packages/upgrader

@Asquere answer is right. @Asquere 的答案是正确的。 However if you want it to work with both xxxx.. number and when comparing但是,如果您希望它同时与 xxxx.. 数字和比较时一起使用

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

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

All these answers doesn't work for me, so I created my own solution, because I'm work with this differents "versions" values types: '11' , '11.1' , '11.0.0.0.0.1' , etc....所有这些答案对我都不起作用,所以我创建了自己的解决方案,因为我正在使用这种不同的“版本”值类型: '11''11.1''11.0.0.0.0.1'等。 ..

The solution is:解决办法是:

void main() {
  String firstVersion = '11.0.1', secondVersion = '11.1';

  final List<String> first = firstVersion.split('.');
  final List<String> second = secondVersion.split('.');
  bool isHigher = false;
  
   // Here im equaliting the length of the versions
  if (first.length > second.length) {
    final size = first.length - second.length;
    for (var i = 0; i < size; i++) {
      second.add('0');
    }
  } else {
    final size = second.length - first.length;
    for (var i = 0; i < size; i++) {
      first.add('0');
    }
  }

  // Here im comparing the versions
  for (var i = 0; i < (first.length); i++) {
    isHigher =
        int.parse(first[i]) >= int.parse(second[i]);
    if (int.parse(first[i]) != int.parse(second[i])){
      break;
    }
  }

  print("Versions:");
  print('a = $firstVersion \nb = $secondVersion');
  print('\nIs a higher than b?');
  print(isHigher);
}

Check the code here: https://dartpad.dev/?id=2656fd69d825844d9415fc08810554d4在此处查看代码: https ://dartpad.dev/?id=2656fd69d825844d9415fc08810554d4

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

试试这个, https://pub.dev/packages/upgrader它将帮助您强制更新应用程序

您可以将应用程序版本转换为 int

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM