这是我的功能:
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的一个实例,我想使用此信息来创建标记,我该怎么做?
答案 0 :(得分:0)
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}