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

72qzrwbm  于 2022-12-19  发布在  Flutter
关注(0)|答案(9)|浏览(114)

我试图比较Flutter中的两个应用版本:

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

我如何说明哪个版本比另一个大?

kcrjzv8t

kcrjzv8t1#

将它们作为整数进行比较!

一个很酷的解决方案是将每个版本转换为指数整数,这样我们就可以简单地将它们作为整数进行比较!

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

在Dartpad上查看此**running example**。

z9ju0rcb

z9ju0rcb2#

由于版本号和修订号可以超过9。我们经常有版本号像“1.13.11”,“211.74.23”等。一个解决办法是分裂和比较个别号码。

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

pu3pd22g3#

使用www.example.com上的version软件包pub.dev
关于这个包裹的一些信息
第一个月
可以使用此代码比较和执行任务

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

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

esyap4oy4#

根据pub.dev上的此软件包:第一个月

import 'package:package_info_plus/package_info_plus.dart';

PackageInfo packageInfo = await PackageInfo.fromPlatform();

String version = packageInfo.version;

该版本是String格式,因此您可以使用.“"拆分该字符串,并获取单个int,然后分别比较它们,如果需要,可以使用example

xt0899hw

xt0899hw5#

@Asquere答案是正确的。但是,如果您希望它同时适用于x.x.x.x...数字和比较时

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

ukxgm1gy6#

另一个简单的方法是使用Version插件来帮助比较版本,它也可以处理很多边缘情况。
像这样使用它:-

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

更新日期:-

只是让别人知道我在我的生产应用中使用这种方法已经有很长一段时间了。我在GitHub版本中推送了很多更新,我的应用使用这个包来比较GitHub版本的当前版本和新版本。TBH我从来没有遇到过任何问题。真的很棒的包。

9jyewag0

9jyewag07#

所有这些答案对我都不起作用,所以我创建了自己的解决方案,因为我使用的是不同的"版本"值类型:* *"11""11.1""11.0.0.0.0.1"**等等。
解决方法是:

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

检查代码:https://dartpad.dev/?id=2656fd69d825844d9415fc08810554d4

bgtovc5b

bgtovc5b8#

好吧,我有个临时解决办法,

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

0aydgbwb9#

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

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

相关问题