如何从列表<Marker>和构建标记中获取信息



这是我的函数:

Future <List <Markers>> getMarker() async {
String myUrl = "http://10.25.84.36:5000/api/markers";
var response = await http.get(myUrl,
headers: {
'Accept': 'application/json',
});
var jsonData = json.decode(response.body);
List<Markers> markers =[];
var u;
for( u in jsonData){
u.forEach((v) { Markers marker = Markers(v["id"],v["lag"],v["log"]);
markers.add(marker); });

}
print(markers.length);
print(markers);
return markers;

}

它返回一个 Future 的实例,我想使用这些信息来创建标记,我该怎么做?

class Markers {
int id;
int lag;
String log;
Markers({this.id, this.lag, this.log});
Markers.fromJson(Map<String, dynamic> json) {
id = json['id'];
lag = json['lag'];
log = json['log'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['lag'] = this.lag;
data['log'] = this.log;
return data;
}
}
Future <List<Markers>> getMarker() async {
String myUrl = "http://10.25.84.36:5000/api/markers";
var response = await http.get(myUrl,
headers: {
'Accept': 'application/json',
});
var jsonData = json.decode(response.body);
List<Markers> markers = [];
// assuming you have the following json: [{"id": 1, "lag": 100, "log": "bla"}, {"id": 2, "lag": 110, "log": "bla2 "}, {" id ": 3," lag ": 90," log ":" bla "}]
jsonData.forEach((v) => markers.add(Markers.fromJson(v)));

print(markers.length);
markers.forEach((v) => print(v.toJson()));
return markers;
}

forEach 对列表中的每个元素运行一个函数。 在这种情况下,映射 {id: x, lag: y, log: z}

相关内容

  • 没有找到相关文章

最新更新