【发布时间】:2018-07-07 16:52:36
【问题描述】:
我正在尝试在 Flutter 中从 Internet 获取数据。 但我在 JSON 解析时遇到错误。 谁能告诉我是什么问题?
我正在尝试从此 URL 获取数据
https://swapi.co/api/starships/
示例 JSON
{ “计数”:37, "下一个": "https://swapi.co/api/starships/?page=2", “上一个”:空, “结果”: [ { "name": "执行者", "model": "执行者级星无畏", “制造商”:“Kuat Drive Yards,Fondor Shipyards”, "cost_in_credits": "1143350000", “长度”:“19000”, “最大大气速度”:“不适用”, “船员”:“279144”, “乘客”:“38000”, "cargo_capacity": "250000000", “消耗品”:“6 年”, “hyperdrive_rating”:“2.0”, “MGLT”:“40”, "starship_class": "星际无畏", “飞行员”:[], “电影”:[ “https://swapi.co/api/films/2/”, “https://swapi.co/api/films/3/” ], “创建”:“2014-12-15T12:31:42.547000Z”, “已编辑”:“2017-04-19T10:56:06.685592Z”, “网址”:“https://swapi.co/api/starships/15/” }, ] }模型类
class RestModel {
final String name;
final String model;
final String manufacturer;
final String cost_in_credits;
final String length;
final String max_atmosphering_speed;
final String crew;
final String passengers;
final String cargo_capacity;
final String consumables;
final String hyperdrive_rating;
final String MGLT;
final String starship_class;
final List films;
final String pilots;
final String created;
final String edited;
final String url;
RestModel(
{this.name,
this.model,
this.manufacturer,
this.cost_in_credits,
this.length,
this.max_atmosphering_speed,
this.crew,
this.passengers,
this.cargo_capacity,
this.consumables,
this.hyperdrive_rating,
this.MGLT,
this.starship_class,
this.films,
this.pilots,
this.created,
this.edited,
this.url});
factory RestModel.fromJson(Map<String, dynamic> json) {
return RestModel(
name: json["name"],
model: json["model"],
manufacturer: json["manufacturer"],
cost_in_credits: json["cost_in_credits"],
max_atmosphering_speed: json["max_atmosphering_speed"],
crew: json["crew"],
passengers: json["passengers"],
cargo_capacity: json["cargo_capacity"],
consumables: json["consumables"],
hyperdrive_rating: json["hyperdrive_rating"],
MGLT: json["MGLT"],
starship_class: json["starship_class"],
films: json["flims"],
pilots: json["pilots"],
created: json["created"],
edited: json["edited"],
url: json["url"],
);
}
}
Flutter 代码是:
final link = "https://swapi.co/api/starships/";
List<RestModel> list;
Future getData() async {
var res = await http
.get(Uri.encodeFull(link), headers: {"Accept":"application/json"});
if (res.statusCode == 200) {
var data = json.decode(res.body);
var rest = data["results"];
for (var model in rest) {
list.add(RestModel.fromJson(model));
}
print("List Size: ${list.length}");
}
}
主要问题是当它尝试从 JSON 填充数据时。
RestModel.fromJson(model)
所以我必须改变什么来解决这个问题。
【问题讨论】:
-
pilots 在您的模型中被声明为字符串,而它的列表在 json 中。
-
还是一样的结果,
-
我在 dart 中尝试这段代码,
void main() async{ final link = "https://swapi.co/api/starships/"; List<RestModel> list; var res = await http .get(Uri.encodeFull(link), headers: {"Accept": "application/json"}); if (res.statusCode == 200) { var data = json.decode(res.body); var rest = data["results"]; for (var json in rest) { list.add(RestModel.fromJson(json)); } print("List Size: ${list.length}"); } },它给出了这个错误 NoSuchMethodError: No static method 'fromJson' declaration in class 'RestModel'。