开发者问题收集

如何从代码中获取 Flutter 版本?

2017-09-01
3886

如何从代码中获取 Flutter 版本?

我想在 API UserAgent 和日志中包含 Flutter 版本。

1个回答

我猜你需要一个自定义构建脚本来创建一个类似

lib/src/flutter_version.dart

的文件,其内容类似

const String version = const <String,String>
{
  "channel": "alpha",
  "repositoryUrl": "https://github.com/flutter/flutter.git",
  "frameworkRevision": "d36e2f6191793de66e0a132ad8c86885829bc6b2",
  "frameworkCommitDate": "2017-06-21 15:09:10 -0700",
  "engineRevision": "b0dee695ecb9ea2438f4d74afdca45839858c311",
  "dartSdkVersion": "1.24.0-dev.6.7"
};

你可以通过

echo "const String version = const \<String,String\>" > lib/src/flutter_version.dart
flutter --version --machine >> lib/src/flutter_version.dart
echo ";" >> lib/src/flutter_version.dart

创建它,然后你可以导入它并读取值

import 'package:my_package/src/flutter_version';

main() {
  print(version['frameworkRevision']);
}

对于其他设备信息,还有 https://github.com/flutter/plugins/tree/master/packages/device_info

Günter Zöchbauer
2017-09-01