【发布时间】:2020-11-28 07:41:09
【问题描述】:
下午好,我正在调用我的 api 来带上我注册的企业。
这是我的模特
class Shops{
Shops({
this.id,
this.name,
this.phone,
this.merchantType,
this.address,
this.city,
this.distance,
this.logo,
});
String id;
String name;
String phone;
String merchantType;
String address;
String city;
double distance;
String logo;
factory Shops.fromJson(Map<String, dynamic> json){
return Shops(
id: json["_id"],
name : json["name"],
phone : json["phone"],
merchantType : json["merchantType"],
address : json["address"],
city : json["city"],
distance : json["distance"].toDouble(),
logo : json["logo"],
);
}
}
我来打个电话
Future<Shops> getShops(var userLatitude, var userLongitude) async {
final response =
await http.get('https://dry-plateau-30024.herokuapp.com/v1/front/merchants?lat=$userLatitude&lng=$userLongitude');
if (response.statusCode == 200) {
return Shops.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load Shops');
}
}
所以我显示结果
FutureBuilder<Shops>(
future:
getShops(services.userLatitude, services.userLongitude),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.name);
} else if (snapshot.hasError) {
return Information(
title: 'No results found',
subtitle: 'It seems there is nothing in your area',
img: 'assets/nada.png',
);
} else {
return CircularProgressIndicator();
}
},
)
这会返回以下错误
_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
我正在随着框架和飞镖语言的发展而发展,我遇到了这些错误,对你来说可能是新手。我会很感激一些帮助
【问题讨论】:
-
你能告诉我们你的 JSON 是什么样子的吗? JSON 的设置方式将决定我们如何访问它。如果它是一个 List,那么你将不得不通过它进行映射。