简体   繁体   English

如何在应用程序中获取 Dart 和 Flutter 版本

[英]How to get Dart and Flutter version within the app

How to log() the version of Dart and Flutter within the app?如何在应用程序中log() DartFlutter的版本?

like:喜欢:

String dartVersion = ?;
log("dart: $dartVersion");

To do the dart version, you can use the Platform class to return the dart version directly as this example shows:要执行 dart 版本,您可以使用 Platform 类直接返回 dart 版本,如下例所示:

import 'dart:io' show Platform;
var version = Platform.version;
print("version:"+version);

As far as I know, there isn't a direct way to get the flutter version, but you can make a custom build script in order to get it based on the following CLI command:据我所知,没有直接的方法来获取 flutter 版本,但您可以制作自定义构建脚本,以便基于以下 CLI 命令获取它:

flutter --version --machine

and use its output to create a dart file at build time that we can import into the project.并在构建时使用其输出创建一个 dart 文件,我们可以将其导入到项目中。 Once there, we can read it directly at runtime.在那里,我们可以在运行时直接读取它。

So, we would make the following build script to create the dart file (named flutterVersion.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.

This would create the following file:这将创建以下文件:

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

Then, you would just import that file and read it at run time:然后,您只需导入该文件并在运行时读取它:

import 'package:my_package/src/flutterVersion.dart';

main() {
  print(version['channel']);
  print(version['frameworkVersion']);
}

//output: 
//master
//1.15.19-pre.9

I adapted the solution of William Terril to a dart script.我将 William Terril 的解决方案改编为飞镖脚本。

import 'dart:io' show Process, ProcessResult;
import 'dart:convert' show json;

void main() {
  Process.run('flutter', ['--version', '--machine']).then(
    (ProcessResult results) {
      final result = Map<String, Object>.from(
        json.decode(results.stdout.toString()) as Map,
      );
      print(constantDeclarationsFromMap(result, 'kFlutter'));
    },
  );
}

String constantDeclarationsFromMap(Map<String, Object> map,
    [String prefix = 'k']) {
  String _capitalize(String text) =>
      text.isEmpty ? text : "${text[0].toUpperCase()}${text.substring(1)}";

  String _constantName(String name, String prefix) =>
      prefix.isEmpty ? name : prefix + _capitalize(name);

  return map.entries
      .map((e) =>
          'const ${_constantName(e.key, prefix)} = ${json.encode(e.value)};')
      .join('\n');
}

Then running something like dart get_flutter_info.dart > flutter_info.dart I will get a file that looks like this:然后运行类似dart get_flutter_info.dart > flutter_info.dart东西,我会得到一个如下所示的文件:

const kFlutterChannel = "beta";
const kFlutterDartSdkVersion = "2.9.0 (build 2.9.0-21.10.beta)";
const kFlutterEngineRevision = "d6ee1499c27a156a797d9f1539ffb7892855c1d0";
const kFlutterFrameworkCommitDate = "2020-08-01 09:01:12 -0700";
const kFlutterFrameworkRevision = "916c3ac648aa0498a70f32b5fc4f6c51447628e3";
const kFlutterFrameworkVersion = "1.20.0";
const kFlutterRepositoryUrl = "https://github.com/flutter/flutter.git";

So you can take advantage of the types and autocomplete.因此,您可以利用类型和自动完成功能。 I wish there was a nice way to inject this code into the build process.我希望有一种很好的方法可以将这段代码注入到构建过程中。

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

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