要执行 dart 版本,您可以使用 Platform 类直接返回 dart 版本,如以下示例所示:
import 'dart:io' show Platform;
var version = Platform.version;
print("version:"+version);
据我所知,没有直接的方法来获取 flutter 版本,但是您可以制作自定义构建脚本,以便根据以下 CLI 命令获取它:
flutter --version --machine
并使用其输出在构建时创建一个 dart 文件,我们可以将其导入到项目中。一旦到达那里,我们就可以在运行时直接读取它。
因此,我们将创建以下构建脚本来创建 dart 文件(名为 flutterVersion.dart):
rm lib/src/flutterVersion.dart # <-- prevents appending to an old file.
echo "Building flutterVersion.dart"
### the following creates a dart file, and appends the output of
### <flutter --version --machine> to a Map variable named "version",
### and finally appends a semicolon at the end.
echo "const Map<String,String> version = " >> lib/src/flutterVersion.dart
flutter --version --machine >> lib/src/flutterVersion.dart
echo ";" >> lib/src/flutterVersion.dart
echo "Continuing flutter build"
// put other build commands here.
这将创建以下文件:
const Map<String,String> version =
{
"frameworkVersion": "1.15.19-pre.9",
"channel": "master",
"repositoryUrl": "https://github.com/flutter/flutter.git",
"frameworkRevision": "e13e17009dcb009f12335eb281f7295ba42de771",
"frameworkCommitDate": "2020-03-06 21:38:35 -0800",
"engineRevision": "5aff3119480996ca014ec0f8d26d74db617b5852",
"dartSdkVersion": "2.8.0 (build 2.8.0-dev.12.0 9983424a3c)"
};
然后,您只需导入该文件并在运行时读取它:
import 'package:my_package/src/flutterVersion.dart';
main() {
print(version['channel']);
print(version['frameworkVersion']);
}
//output:
//master
//1.15.19-pre.9