简体   繁体   English

“字符串”类型不是“列表”类型的子类型<latlng> ' 我如何将闲置的 flutter 字符串转换为 LatLng 以使用 Polygon</latlng>

[英]type 'String' is not a subtype of type 'List<LatLng>' how do i convert the fallowing flutter string to LatLng for the use of Polygon

i am recoiving this data from API and i have to use as its, tried to split but then i get several errors //This data is string i would like to convert to it LatLng我正在从 API 中重新获取这些数据,我必须将其用作它,尝试拆分但随后出现几个错误//此数据是我想转换为它的字符串 LatLng

var polypoints =
        "[LatLng(8.549726145655804, 47.30110634118318), LatLng(8.54721166008782, 47.301803715527065), LatLng(8.548983472319675, 47.30491507798434), LatLng(8.550479427132618, 47.30423916131258)]";
   // i have tried this i will get error 
    List<LatLng> latlngList = jsonDecode(polypoints);

   //Unhandled Exception: type 'String' is not a subtype of type 'List<LatLng>'

    List<LatLng> latlngList = jsonDecode(polypoints);
    var polygon = Polygon(
      points: polypoints,   //there is error here i have to use only LatLng
      strokeColor: Colors.red,
      strokeWidth: 2,
      fillColor: Colors.blue,
    );
    // i would like to be able to use as variable in the point below
    List<LatLng> pointst = [
      LatLng(8.549726145655804, 47.30110634118318),
      LatLng(8.54721166008782, 47.301803715527065),
      LatLng(8.548983472319675, 47.30491507798434),
      LatLng(8.550479427132618, 47.30423916131258)
    ];

     var polygoncorrect = Polygon(
      points: pointst,       //in here its same data but no i am hard coding it
      strokeColor: Colors.red,
      strokeWidth: 2,
      fillColor: Colors.blue,
    );

thank you谢谢你

First, "[LatLng(8.549726145655804, 47.30110634118318), LatLng(8.54721166008782, 47.301803715527065) is no json.首先, "[LatLng(8.549726145655804, 47.30110634118318), LatLng(8.54721166008782, 47.301803715527065)没有 json。

It would need to look like { "LatLng": [ "Lat: 8.5", "Lng: 47.3" ] }它需要看起来像{ "LatLng": [ "Lat: 8.5", "Lng: 47.3" ] }

Then you would create a class for LatLng and create method to create the object:然后,您将为 LatLng 创建一个 class 并创建创建 object 的方法:

class LatLng {
  float lat;
  float lng;
  LatLng(this.lat, this.lng);
  factory LatLng.fromJson(dynamic json) {
    return LatLng(json['lat'] as float, json['lng'] as float);
  }
  @override
  String toString() {
    return '{ ${this.lat}, ${this.lng} }';
  }
}

Haven't done anything in Flutter, lately, but this should more or less work.最近在 Flutter 中没有做任何事情,但这应该或多或少起作用。 I quickly searched for similar issues, here, and adapted the code.我在这里快速搜索了类似的问题,并修改了代码。 Key words would be json decrypt nested array class关键词是 json 解密嵌套数组 class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“类型'String'不是类型'(String?)=&gt; String的子类型?” flutter 中的错误 - How to fix "type 'String' is not a subtype of type '(String?) => String?" error in flutter 将字符串转换为Double以在LatLng / Google地图中使用 - Converting String to Double for use in LatLng/Google Maps “字符串”类型不是“列表”类型的子类型<string> '</string> - Type 'String' is not a subtype of type 'List<String>' LatLng对象的分割字符串 - Splitting string for LatLng object &#39;String&#39; 类型不是 &#39;index&#39; Flutter 的 &#39;int&#39; 类型的子类型 - type 'String' is not a subtype of type 'int' of 'index' Flutter “String”类型不是 Flutter 中“Item”类型的子类型 - type 'String' is not a subtype of type 'Item' in Flutter type 'Null' is not a subtype of type 'String' 错误 in Flutter - type 'Null' is not a subtype of type 'String' error in Flutter 如何解决抖动错误字符串不是int类型的子类型 - How to solve flutter error String is not a subtype of type int “字符串”类型不是“列表”类型的子类型<String> &#39; 在类型转换中? - type 'String' is not a subtype of type 'List<String>' in type cast? 在 flutter 中解码字符串 json 时出错,“字符串类型不是 Map 类型的子类型” - Error decoding string json in flutter, “type String is not subtype of type Map”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM