简体   繁体   中英

How to get Dart and Flutter version within the app

How to log() the version of Dart and Flutter within the app?

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:

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 --version --machine

and use its output to create a dart file at build time that we can import into the project. 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):

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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