[英]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.