version.dart 836 B

12345678910111213141516171819202122232425262728293031
  1. import 'package:sport/services/Converter.dart';
  2. List<int> version(String versionName) {
  3. if (versionName.isEmpty == true) return [0, 0, 0];
  4. var arr = versionName.split(".");
  5. while (arr.length < 3) arr.add("0");
  6. return arr.map((e) => Converter.toInt(e)).toList();
  7. }
  8. /**
  9. * v1: local
  10. * v2: net
  11. */
  12. int versionCompare(String v1, String v2) {
  13. if (v1 == v2) return 0;
  14. var clientVersion = version(v1);
  15. var baseVersion = version(v2);
  16. int client =
  17. (clientVersion[0] << 20) | (clientVersion[1] << 10) | clientVersion[2];
  18. int base = (baseVersion[0] << 20) | (baseVersion[1] << 10) | baseVersion[2];
  19. int result = 0;
  20. if (client > base) {
  21. result = 1;
  22. } else if (client == base) {
  23. result = 0;
  24. } else {
  25. result = -1;
  26. }
  27. print("versionCompare $clientVersion $baseVersion == $result");
  28. return result;
  29. }