MatexCountryMetadata.fromJson constructor

MatexCountryMetadata.fromJson(
  1. String id,
  2. Map<String, dynamic> json
)

Creates a MatexCountryMetadata instance from a JSON map.

Throws ArgumentError if id, currency, or code is either null, not a string, or an empty string. Converts the raw VAT rates into a list of doubles.

Implementation

factory MatexCountryMetadata.fromJson(String id, Map<String, dynamic> json) {
  final rawVateRates = json['vatRates'];
  final currency = json['currency'];
  final code = json['code'];
  List<double>? vatRates;

  if (id.isEmpty) {
    throw ArgumentError.value(
      id,
      'id',
      'Must be a non-empty string.',
    );
  }

  if (currency == null || currency is! String || currency.isEmpty) {
    throw ArgumentError.value(
      currency,
      'currency',
      'Must be a non-empty string.',
    );
  }

  if (code == null || code is! String || code.isEmpty) {
    throw ArgumentError.value(
      code,
      'code',
      'Must be a non-empty string.',
    );
  }

  if (rawVateRates != null && rawVateRates is List) {
    vatRates = rawVateRates
        .whereType<num>()
        .map((value) => value.toDouble())
        .toList();
  }

  return MatexCountryMetadata(
    vatRates: vatRates,
    currency: currency,
    code: code,
    id: id,
  );
}