fromJson static method

Plant? fromJson(
  1. dynamic value
)

Returns a new Plant instance and imports its values from value if it's a Map, null otherwise.

Implementation

// ignore: prefer_constructors_over_static_methods
static Plant? fromJson(dynamic value) {
  if (value is Map) {
    final json = value.cast<String, dynamic>();

    // Ensure that the map contains the required keys.
    // Note 1: the values aren't checked for validity beyond being non-null.
    // Note 2: this code is stripped in release mode!
    assert(() {
      requiredKeys.forEach((key) {
        assert(json.containsKey(key),
            'Required key "Plant[$key]" is missing from JSON.');
        assert(json[key] != null,
            'Required key "Plant[$key]" has a null value in JSON.');
      });
      return true;
    }());

    final regex = RegExp(r'\(([-\d.]+),([-\d.]+)\)');
    final matches =
        regex.allMatches(mapValueOfType<String>(json, r'address')!).toList();

    double? longitude;
    double? latitude;
    String? extractedAddress;
    if (matches.isNotEmpty) {
      extractedAddress = mapValueOfType<String>(json, r'address')!.substring(
        0,
        matches.last.start,
      );
      longitude = double.parse(matches.last.group(1)!);
      latitude = double.parse(matches.last.group(2)!);
    }

    return Plant(
      id: num.parse('${json[r'id']}'),
      name: mapValueOfType<String>(json, r'name'),
      address: extractedAddress ?? mapValueOfType<String>(json, r'address'),
      groupId: mapValueOfType<String>(json, r'groupId'),
      group: AssetsGroup.listFromJson(json[r'group']),
      description: mapValueOfType<String>(json, r'description'),
      ingestionConfigData:
          IngestionConfig.fromJson(json['ingestionConfigData']),
      longitude: longitude,
      latitude: latitude,
      createdAt: mapDateTime(json, r'createdAt', r''),
      updatedAt: mapDateTime(json, r'updatedAt', r''),
      gateways: Gateway.listFromJson(json[r'gateways']),
    );
  }
  return null;
}