decodeIconData static method

HWIconData decodeIconData(
  1. DartObject obj
)

Decodes an HWIconData, reading every icon's name, codepoint and font.

Throws a GeneratorError when the icons are unusable: something other than an IconData in the list, icons from more than one font, no icons at all, a duplicate name, or a default or preview icon that is not one of them.

Implementation

static HWIconData decodeIconData(DartObject obj) {
  final key = getField(obj, 'key')?.toStringValue() ?? '';
  final rawIcons = getField(obj, 'icons')?.toListValue() ?? const [];

  final entries = <HWIconEntry>[];
  HWIconFont? font;
  for (final rawIcon in rawIcons) {
    final codePoint = decodeIconCodePoint(rawIcon);
    final iconFont = decodeIconFont(rawIcon);
    if (codePoint == null || iconFont == null) {
      throw GeneratorError(
        'HWIconData "$key" takes Flutter IconData values such as '
        'Icons.wb_sunny, got: ${rawIcon.type?.element?.name}',
      );
    }
    if (font != null && font != iconFont) {
      throw GeneratorError(
        'The icons of HWIconData "$key" come from more than one font '
        '($font and $iconFont). Every icon of one field is subset out of the '
        'same font file, so they all have to share it.',
      );
    }
    font = iconFont;
    entries.add(
      HWIconEntry(
        decodeIconName(rawIcon, codePoint),
        codePoint,
        matchTextDirection: decodeIconMatchTextDirection(rawIcon),
      ),
    );
  }

  if (font == null) {
    throw GeneratorError('HWIconData "$key" needs at least one icon.');
  }

  final icons = HWIconData.resolved(
    key,
    entries: entries,
    iconFont: font,
    defaultValue: _decodeNamedIcon(obj, 'defaultIcon', key: key),
    previewValue: _decodeNamedIcon(obj, 'previewIcon', key: key),
  );
  icons.validate();
  return icons;
}