getDartBinVersion function

Future<Version> getDartBinVersion()

Parse flutter version

Implementation

Future<Version> getDartBinVersion() async {
  // $ dart --version
  // Linux: Dart VM version: 2.7.0 (Unknown timestamp) on "linux_x64"
  var cmd = DartCmd(['--version']);
  // Take from stderr first
  var resultOutput = (await runCmd(cmd)).stderr.toString().trim();
  if (resultOutput.isEmpty) {
    resultOutput = (await runCmd(cmd)).stdout.toString().trim();
  }
  var output = LineSplitter.split(resultOutput)
      .join(' ')
      .split(' ')
      .map((word) => word?.trim())
      .where((word) => word?.isNotEmpty ?? false);
  var foundDart = false;
  try {
    for (var word in output) {
      if (foundDart) {
        try {
          var version = Version.parse(word);
          if (version != null) {
            return version;
          }
        } catch (_) {}
      }
      if (word.toLowerCase().contains('dart')) {
        foundDart = true;
      }
    }
  } catch (_) {}
  return null;
}