fromJson static method

CanvasFill fromJson(
  1. Map<String, Object?> json
)

Implementation

static CanvasFill fromJson(Map<String, Object?> json) {
  final type = json['type'];
  if (type is! String) {
    throw const FormatException('CanvasFill requires string field "type"');
  }

  switch (type) {
    case 'none':
      return const CanvasFillNone();

    case 'solid':
      final c = json['color'];
      if (c is! num) {
        throw const FormatException(
          'CanvasFillSolid requires numeric "color"',
        );
      }
      return CanvasFillSolid(c.toInt());

    case 'gradient':
      final g = json['grad'];
      if (g is! Map) {
        throw const FormatException('CanvasFillGradient requires map "grad"');
      }
      return CanvasFillGradient(
        LinearGradientSpec.fromJson(g.cast<String, dynamic>()),
      );

    default:
      throw FormatException('Unknown CanvasFill type: $type');
  }
}