FeatureCollection.fromJson constructor Null safety

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

Creates a FeatureCollection from a JSON object.

Implementation

factory FeatureCollection.fromJson(Map<String, dynamic> json) {
  if (json['type'] != 'FeatureCollection') {
    throw ArgumentError('json is not a FeatureCollection');
  }

  return FeatureCollection(
    (json['features'] as List<Map<String, dynamic>>)
        .map((Map<String, dynamic> f) {
      if (f['geometry']['type'] == 'Point') {
        return Point.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiPoint') {
        return MultiPoint.fromJson(f);
      } else if (f['geometry']['type'] == 'LineString') {
        return LineString.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiLineString') {
        return MultiLineString.fromJson(f);
      } else if (f['geometry']['type'] == 'Polygon') {
        return Polygon.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiPolygon') {
        return MultiPolygon.fromJson(f);
      } else {
        throw ArgumentError('json is not a feature');
      }
    }).toList(),
  );
}