Map getFlutterInformation()

in packages/snippets/lib/util.dart [74:140]


  Map<String, dynamic> getFlutterInformation() {
    if (_cachedFlutterInformation != null) {
      return _cachedFlutterInformation!;
    }

    String flutterVersionJson;
    if (platform.environment['FLUTTER_VERSION'] != null) {
      flutterVersionJson = platform.environment['FLUTTER_VERSION']!;
    } else {
      String flutterCommand;
      if (platform.environment['FLUTTER_ROOT'] != null) {
        flutterCommand = filesystem
            .directory(platform.environment['FLUTTER_ROOT'])
            .childDirectory('bin')
            .childFile('flutter')
            .absolute
            .path;
      } else {
        flutterCommand = 'flutter';
      }
      io.ProcessResult result;
      try {
        result = processManager.runSync(
            <String>[flutterCommand, '--version', '--machine'],
            stdoutEncoding: utf8);
      } on io.ProcessException catch (e) {
        throw SnippetException(
            'Unable to determine Flutter information. Either set FLUTTER_ROOT, or place flutter command in your path.\n$e');
      }
      if (result.exitCode != 0) {
        throw SnippetException(
            'Unable to determine Flutter information, because of abnormal exit to flutter command.');
      }
      flutterVersionJson = (result.stdout as String).replaceAll(
          'Waiting for another flutter command to release the startup lock...',
          '');
    }

    final Map<String, dynamic> flutterVersion =
        json.decode(flutterVersionJson) as Map<String, dynamic>;
    if (flutterVersion['flutterRoot'] == null ||
        flutterVersion['frameworkVersion'] == null ||
        flutterVersion['dartSdkVersion'] == null) {
      throw SnippetException(
          'Flutter command output has unexpected format, unable to determine flutter root location.');
    }

    final Map<String, dynamic> info = <String, dynamic>{};
    info['flutterRoot'] =
        filesystem.directory(flutterVersion['flutterRoot']! as String);
    info['frameworkVersion'] =
        Version.parse(flutterVersion['frameworkVersion'] as String);

    final RegExpMatch? dartVersionRegex =
        RegExp(r'(?<base>[\d.]+)(?:\s+\(build (?<detail>[-.\w]+)\))?')
            .firstMatch(flutterVersion['dartSdkVersion'] as String);
    if (dartVersionRegex == null) {
      throw SnippetException(
          'Flutter command output has unexpected format, unable to parse dart SDK version ${flutterVersion['dartSdkVersion']}.');
    }
    info['dartSdkVersion'] = Version.parse(
        dartVersionRegex.namedGroup('detail') ??
            dartVersionRegex.namedGroup('base')!);
    _cachedFlutterInformation = info;

    return info;
  }