chuyentt / geojson_vi

An Open-Source Dart and Flutter Library for Efficiently Handling GeoJSON Data in Compliance with RFC 7946
https://pub.dev/packages/geojson_vi
MIT License
15 stars 10 forks source link

Fix empty map casting #17

Closed uSlashVlad closed 2 years ago

uSlashVlad commented 2 years ago

About the issue

I found some interesting issue today that breaks behavior of GeoJSONFeature.fromMap(...) The issue is happening because empty map like {} has type _InternalLinkedHashMap and not Map (like maps from JSON converting) and so feature with empty map in "properties" field creates this error:

Unhandled exception:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>?'
#0      new GeoJSONFeature.fromMap                  package:geojson_vi/…/classes/feature.dart:51

but map like <String, dynamic>{} works well because it has type Map<String, dynamic> and it is ok. Omitting "properties" field and non empty maps works well too.

Issue also happens with GeoJSONFeatureCollection.fromMap(...) since it uses GeoJSONFeature.fromMap(...).

Example

import 'package:geojson_vi/geojson_vi.dart';

void main() async {
  final features = {
    "type": "Feature",
    "properties": {},
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [34.453125, 54.007768],
          [40.78125, 54.007768],
          [40.78125, 57.326521],
          [34.453125, 57.326521],
          [34.453125, 54.007768],
        ],
      ],
    },
  };
  final collectionGeoJSON = GeoJSONFeature.fromMap(features);
}

Way to fix it

It can be fixed quite easily with explicit map casting using Map.castFrom(...) so empty "properties" map will work well too, so I made this fix in this pull request: In file lib/src/classes/feature.dart in line 51 I changed map['properties'] to Map.castFrom(map['properties']) so it always will be converted to Map<String, dynamic> before passing into GeoJSONFeature constructor.