Home > Software design >  type 'string' is not a subtype of type 'list<latlng>'
type 'string' is not a subtype of type 'list<latlng>'

Time:09-23

I try to show map polygon. I get list of LatLng data from json.

var map = widget.list[widget.index]['map'];

data is already decoded

Sample:

  Set<Polygon> myPolygon() {

  Set<Polygon> polygonSet = new Set();
  polygonSet.add(Polygon(
      polygonId: PolygonId('test'),
      points: map,
      strokeWidth: 2,
      strokeColor: Colors.blue,
      fillColor: Colors.blue.withOpacity(0.4),));

  return polygonSet;
}

This code show error : type 'string' is not a subtype of the 'List<LatLng>'

[I want output like this1

CodePudding user response:

you need to pass List<LatLng> to the points of PolyLine make sure you are getting List inside this var map = widget.list[widget.index]['map'];

CodePudding user response:

I don't know this is a correct method or not but it works for me. Remove Latlng & brackets replace by ", then decode the data. after need count of data so I use count variable

  var mapdata1 = map.replaceAll('LatLng(', '"');
    var mapdata2 = mapdata1.replaceAll(')', '"');
    var mapdata3 = mapdata2.replaceAll(',]', ']');
    var mapdata4 = json.decode(mapdata3);
    int count = mapdata4.length;

then created list latLang variable

List<LatLng> latlang =new List();

created for loop to fetch list data

  for (var i = 0; i <= (count-1); i  ) {
    List<String> latlong  =  mapdata4[(i)].split(",");
    double latitude =  double.parse(latlong[0]);
    double longitude = double.parse(latlong[1]);
    latlang.add(LatLng(latitude, longitude));
    }

if need to check the output

print(latlang);

give latLang variable to polygon points

Set<Polygon> myPolygon() {
      Set<Polygon> polygonSet = new Set();
      polygonSet.add(Polygon(
          polygonId: PolygonId('test'),
          points: latlang,
          strokeWidth: 2,
          strokeColor: Colors.grey,
          fillColor: Colors.grey.shade700.withOpacity(0.4),));
      return polygonSet;
    }
  • Related