ReportTemplate.fromJson constructor

ReportTemplate.fromJson(
  1. Map<String, dynamic> json
)

Restores an unwrapped template from decoded JSON.

Implementation

factory ReportTemplate.fromJson(Map<String, dynamic> json) {
  final page = json['page'] as Map<String, dynamic>? ?? const {};
  final format = (page['format'] ?? 'A4').toString().toLowerCase();
  final paperSize = ReportPaperSize.values.firstWhere(
    (value) => value.name == format,
    orElse: () => ReportPaperSize.a4,
  );
  final orientationName = page['orientation']?.toString().toLowerCase();
  final width = (page['width'] as num?)?.toDouble();
  final height = (page['height'] as num?)?.toDouble();
  final inferredOrientation =
      width != null && height != null && width > height
      ? ReportOrientation.landscape
      : ReportOrientation.portrait;
  final orientation = orientationName == null
      ? inferredOrientation
      : ReportOrientation.values.firstWhere(
          (value) => value.name == orientationName,
          orElse: () => inferredOrientation,
        );
  return ReportTemplate(
    name: json['name'] ?? 'Import',
    paperSize: paperSize,
    orientation: orientation,
    headerHeight: (page['headerHeight'] ?? 72).toDouble(),
    footerHeight: (page['footerHeight'] ?? 55).toDouble(),
    locale: json['locale'] ?? 'de-DE',
    currency: json['currency'] ?? 'EUR',
    variables: Map<String, dynamic>.from(json['variables'] ?? const {}),
    fonts: (json['fonts'] as List<dynamic>? ?? const [])
        .map((font) => ReportFont.fromJson(font as Map<String, dynamic>))
        .toList(),
    elements: (json['elements'] as List)
        .map((element) => ReportElement.fromJson(element))
        .toList(),
  );
}