discontinuedPackage magic_yaml_config has been discontinued.

printValue function

void printValue(
  1. Object? value,
  2. OutputFormat outputFormat, {
  3. String? listPrefix,
})

Выводит значение в указанном формате.

Поддерживаются следующие форматы:

При использовании OutputFormat.auto формат определяется автоматически. Для List применяется OutputFormat.lines, для Map используется json формат. Остальные значения выводятся в формате OutputFormat.raw.

Через параметр listPrefix может быть указана строка, вставляемая в начало элементов списка.

Implementation

void printValue(
  Object? value,
  OutputFormat outputFormat, {
  String? listPrefix,
}) {
  if (value == null) {
    return;
  }

  if (outputFormat == OutputFormat.auto) {
    if (value is List) {
      outputFormat = OutputFormat.lines;
    } else if (value is Map) {
      outputFormat = OutputFormat.json;
    } else {
      outputFormat = OutputFormat.raw;
    }
  }

  switch (outputFormat) {
    case OutputFormat.raw:
      print(value);
      return;

    case OutputFormat.json:
      print(jsonEncode(value));
      return;

    case OutputFormat.joined:
      if (value is! List) {
        throw FormatException(
          "The 'joined' format requires a YAML list.",
        );
      }

      print(value.map((item) => '${listPrefix ?? ''}$item').join(' '));

      return;

    case OutputFormat.lines:
      if (value is! List) {
        throw FormatException("The 'lines' format requires a YAML list.");
      }

      for (final item in value) {
        print('${listPrefix ?? ''}$item');
      }

      return;

    case OutputFormat.auto:
      throw StateError('auto format was not resolved.');
  }
}